Reputation: 159
Can someone help me in converting this dataweave to target format ? basically convert the items into array.
I even started , but not sure how to proceed further. any suggestions will be helpful
payload mapObject(order,key,index) -> {
((key):order) if ! (key contains "Item_"),
}
input:
{
"id": "1",
"Item_1_ID": "43-583-0978",
"Item_1_Name": "Sodium Chloride",
"Item_1_Quantity": "26",
"Item_1_Price": "802.41",
"Item_2_ID": "71-788-5293",
"Item_2_Name": "Ciprofloxacin",
"Item_2_Quantity": "100",
"Item_2_Price": "608.64",
}
Output:
{
"id": "1",
Items:[{
"ID":"43-583-0978",
"Name": "Sodium Chloride",
"Price": "802.41",
"Quantity": "26",
},
{
"ID":"71-788-5293",
"Name": "Ciprofloxacin",
"Price": "100",
"Quantity": "608.64",
}]
}
Upvotes: 2
Views: 72
Reputation: 5059
Here you have the solution for the problem you have requested. It uses some regex to extract the parts of the key and then groupBy and pluck function to group by Id number and pluck to transform the groupBy result into the array and transform the keys.
%dw 2.0
output application/json
fun getItemNumber(keyName:String) =
(keyName scan /Item_(\d+)/)[0][1]
fun getKeyName(keyName:String) =
(keyName scan /Item_[\d+]_(.*)/)[0][1]
---
{
"Id" : payload.id,
"Item": (payload - "id")
groupBy ((value, key) -> //Group it by ID
getItemNumber(key as String)
)
pluck ((value, key, index) -> //Go through all the values of the groups and take the real keys
value mapObject ((value, key, index) -> {
(getKeyName(key)): value
})
)
}
Upvotes: 3