How to turn a string which should be split on the <space> an turned into an array of objects in mulesoft - mule

I am trying to turn a string which should be split on the basis of space and turned into an array of objects.
Please help me how can I form it.
Input
field: YYC:16:26 YVR:16:03 YEG:13:43
Output Expected
"details" : [
{
"field" : "YYC",
"time" : "16:26"
},
{
"field" : "YVR",
"Time" : "16:03"
},
{
"field" : "YEG",
"Time" : "13:43"
}
]

A slight twist to what Karthik posted:
%dw 2.0
output application/json
import * from dw::core::Arrays
var test= "YYC:16:26 YVR:16:03 YEG:13:43" splitBy " "
---
details: test map
{
"field": ($ splitBy ":")[0],
"Time": drop(($ splitBy ":"),1)joinBy ":"
}

you need to split first by sapce and then need to break the remaining string as below
%dw 2.0
output application/json
var test= "YYC:16:26 YVR:16:03 YEG:13:43" splitBy " "
---
details: test map ((item, index) ->
{
"field": item[0 to 2],
"Time": item [4 to -1]
})

Another way of approach similar to Anurag's solution
DW
%dw 2.0
output application/json
var test= "YYC:16:26 YVR:16:03 YEG:13:43" splitBy " "
---
details: test map ((item, index) ->
{
"field": (item splitBy ":")[0],
"Time": (item splitBy ":")[1 to -1] joinBy ":"
})
Output
{
"details": [
{
"field": "YYC",
"Time": "16:26"
},
{
"field": "YVR",
"Time": "16:03"
},
{
"field": "YEG",
"Time": "13:43"
}
]
}

Related

How to iterate over the string using data weave 1.0 and data weave 2.0?

I'm new to dataweave and trying to transform the array and iterate over the "||" values
Input:
[
{
"card":"VISA$$0.0||MASTER$$140.0"
},
{
"card":"VISA$$0.0||MASTER$$147.0"
}
]
The DataWeave code that I tried:
%dw 2.0
output application/json
---
"CardList":payload map (data,index) ->
{
(data.card splitBy "||" map {
"sur": $
})
}
Expected response :
{
"cardList": [
{
"card": "VISA$$0.0"
},
{
"card": "MASTER$$140.0"
},
{
"card": "VISA$$0.0"
},
{
"card": "MASTER$$147.0"
}
]
}
Someone one could you please assist me here on mule 3 and 4.
thanks in advance.
Try as below - iterating through splitBy values (||)
%dw 2.0
output application/json
---
"CardList": flatten(payload map
( ($.card splitBy "||") map(item,index) ->
{
card : item
}))

How to use try function with map in dataweave 2.0

Hi everybody i hope you are well, i have a doubt how can i do to use the try inside map in dataweave, i explain about my issue, i receive a csv file with multiple rows, that rows are group by order first and the second, i use map to transform the data in json ( to join all the rows with the same order) format using a couple colums that colums coming from the csv file, if any colum has empty or null the map fail and broke all the content file, how can i use the try function in dataweave if any group of orders fail only get the order and put in another part of the json and follow with the next order without broke the loop.
Part of the CSV File - Demo:
number,date,upc,quantity,price
1234556,2022-08-04,4015,1,
1234556,2022-08-04,4019,1,2.00
1234556,2022-08-04,4016,1,3.00
1234557,2022-08-04,4015,1,3.00
Dataweave:
%dw 2.0
output application/json
---
payload groupBy ($.number) pluck $ map ( () -> {
"number": $[0].number,
"date": $[0].date,
"items": $ map {
"upc": $.upc,
"price": $.price as Number {format: "##,###.##"} as String {format: "##,###.00"},
"quantity": $.quantity
}
})
Error Message:
Unable to coerce `` as Number using `##,###.##` as format.
NOTE: if put the data in the position "price "= something the the issue are solve in the first row, but i need use the function try or what do you recomend, i cant validate all the elements in csv because this is a demo the complete file has a many columns... if you would has another coment to better my code i'd apreciated.
Expected Result: (I don't know if this is possible)
[
{
"data": [
{
"number":"1234557",
"date":"2022-08-04",
"items":[
{
"upc":"4015",
"price":"3.00",
"quantity":"1"
}
]
}
]
},
{
"Error":[
{
"number":"1234556",
"message":"Unable to coerce `` as Number using `##,###.##` as format."
}
]
}
]
best regards!!
Hi The closer I got from what you asked for was
%dw 2.0
output application/json
import * from dw::Runtime
fun safeMap<T, R>(items: Array<T>, callback: (item:T) -> R ): Array<R | {message: String}> =
items map ((item) -> try(() -> callback(item)) match {
case is {success: false} -> {message: $.error.message as String}
case is {success: true, result: R} -> $.result
})
---
payload
groupBy ($.number)
pluck $
safeMap ((item) -> {
"number": item[0].number,
"date": item[0].date,
"items": item safeMap {
"upc": $.upc,
"price": $.price as Number {format: "##,###.##"} as String {format: "##,###.00"},
"quantity": $.quantity
}
})
This uses a combination of map and try function.
And it outputs
[
{
"number": "1234556",
"date": "2022-08-04",
"items": [
{
"message": "Unable to coerce `` as Number using `##,###.##` as format."
},
{
"upc": "4019",
"price": "2.00",
"quantity": "1"
},
{
"upc": "4016",
"price": "3.00",
"quantity": "1"
}
]
},
{
"number": "1234557",
"date": "2022-08-04",
"items": [
{
"upc": "4015",
"price": "3.00",
"quantity": "1"
}
]
}
]
If you are looking to resolve the value of price if price is null/empty value in the input csv and get rid of the error(which is happening because it cannot format Null values to Number) , try adding default in case of empty/null values and formatting to String only when value exists, like below:
%dw 2.0
output application/json
---
payload groupBy ($.number) pluck $ map ( () -> {
"number": $[0].number,
"date": $[0].date,
"items": $ map {
"upc": $.upc,
"price": ($.price as String {format: "##,###.00"}) default $.price,
"quantity": $.quantity
}
})
Note:
For price, you don't need to convert to Number at all if you want your output as formatted string ultimately.

Need an optimized way to get the required output

Is there an optimized way to trim the beginning and end blank spaces from the below data array field values, have used three approaches, but need a more optimized way.
Note: there might be more than 20 objects in the data array and more than 50 fields for each object. Below payload is just a sample; field values can have digits or strings or dates of any size.
{
"School": "XYZ High school",
"data": [
{
"student Name": "XYZ ",
"dateofAdmission": "2021-06-09 ",
"percentage": "89 "
},
{
"student Name": "ABC ",
"dateofAdmission": "2021-08-04 ",
"percentage": "90 "
},
{
"student Name": "PQR ",
"dateofAdmission": "2021-10-01 ",
"percentage": "88 "
}
]
}
Required output:
{
"School": "XYZ High school",
"data": [
{
"student Name": "XYZ",
"dateofAdmission": "2021-06-09",
"percentage": "89"
},
{
"student Name": "ABC",
"dateofAdmission": "2021-08-04",
"percentage": "90"
},
{
"student Name": "PQR",
"dateofAdmission": "2021-10-01",
"percentage": "88"
}
]
}
Three approaches I've used:
First approach:
%dw 2.0
output application/json
//variable to remove beginning and end blank spaces from values in key:value pairs for data
var payload1 = payload.data map ((value , key ) ->
value mapObject ( ($$ )) : trim($))
---
//constructed the payload
payload - "data" ++ data: payload1 map ((item, index) -> {
(item)
})
Second approach:
%dw 2.0
output application/json
---
payload - "data" ++ data: payload.data map ((value , key ) ->
value mapObject ( ($$ )) : trim($)) map ((item, index) -> {
(item)
})
Third approach:
%dw 2.0
output application/json
---
{
"Name":payload.School,
"data": payload.data map ( $ mapObject (($$):trim($) ) )
}
Another solution using the update operator:
%dw 2.0
output application/json
---
payload update {
case data at .data ->
data map ($ mapObject ((value, key, index) -> (key):trim(value) ))
}
Note that your first two solutions are exactly the same, and both have an unneeded map() at the end that doesn't seem to have any purposes. The third solution is very similar but uses an incorrect key name for the school name (Name instead of School as in the example). There's nothing particularly wrong with each solution other than those minor issues.

Dataweave in Mule - Change value Array of Objects

I get a payload as input in the message transform component. It is an Array with Objcts:
[
{
"enterprise": "Samsung",
"description": "This is the Samsung enterprise",
},
{
"enterprise": "Apple",
"description": "This is the Apple enterprise ",
}
]
I have a variable that replaces the description and the output that I want is:
[
{
"enterprise": "Samsung",
"description": "This is the var value",
},
{
"enterprise": "Apple",
"description": "This is the var value",
}
]
I tried to use:
%dw 2.0
output application/java
---
payload map ((item, index) -> {
description: vars.descriptionValue
})
But it returns:
[
{
"description": "This is the var value",
},
{
"description": "This is the var value",
}
]
Is possible to replace only the description value keeping the rest of the fields? Avoiding adding the other fields in the mapping.
There are many ways to do this.
One way to do it is to first remove the original description field and then add the new one
%dw 2.0
output application/java
---
payload map ((item, index) ->
item - "description" ++ {description: vars.descriptionValue}
)
Otherwise you can use mapObject to iterate over the key-value pairs of each object and with pattern matching add a case for when the key is description.
I prefer this second way of doing it when I want to do many replacements.
%dw 2.0
output application/java
fun process(obj: Object) = obj mapObject ((value, key) -> {
(key): key match {
case "description" -> vars.descriptionValue
else -> value
}
})
---
payload map ((item, index) ->
process(item)
)

How can I transform an array of objects to an array of strings and not lose the key in Dataweave?

Hi I need to transform the following JSON object:
{
"products": [
{
"itemno": "123131",
"description" : "Big Widget",
"attributes": [
{
"color": [
{
"value": "Red",
"codeValue": "RED_NO2"
},
{
"value": "Blue Licorice",
"codeValue": "BLUE-355"
}
]
},
{
"chemicals": [
{
"value": "Red Phosphorous",
"codeValue": "RED_PHOS"
},
{
"value": "Chlorine Bleach",
"codeValue": "CHLRN_BLCH"
}
]
}
]
}
]
}
I am trying to transform this with each attribute having an array of values where their value is the codeValue and it's an array of those string values.
This is the desired output:
{
"products": [
{
"itemno": "123131",
"description: : "Big Widget",
"attributes": [
{
"color": ["RED_NO2", "BLUE-355"]
},
{
"chemicals": ["RED_PHOS", "CHLRN_BLCH"]
}
]
}
]
}
This is the Dataweave. I cannot determine how to get the attribute names (i.e. color, chemicals as keys with the desired data.
There is not a lot of good examples on transforming data with Dataweave and I have spent a lot of time trying to figure this out.
This is one of the dataweaves that got there somewhat, but isn't the solution:
%dw 1.0
%output application/json
---
payload.products map
{
"ItemNo" : $.sku,
"Desc" : $.description,
"Test" : "Value",
"Attributes" : $.attributes map
{
'$$' : $ pluck $.value
}
}
Your help is greatly appreciated.
You can do something like this:
%dw 1.0
%output application/json
%function attributeCodeValues(attributes)
attributes map ((attr) ->
attr mapObject ((values, descriptor) ->
{
(descriptor): values map $.codeValue
}
)
)
---
payload.products map {
"ItemNo" : $.sku,
"Desc" : $.description,
"Test" : "Value",
"Attributes" : attributeCodeValues($.attributes)
}