I am using Anypoint Studio 6.1 and Mule 3.8.1 and in Dataweave I have a CSV file as input and JSON list output.
Multiple records are coming in on the CSV file with the same Product Category Id but will have a different product details associated with it. I want my output to be aggregated for each of these records so I have one object for each Product Category Id which contains a list of the product details for each product under that Product Category id. How can I do this?
Current output:
[
{
"Products": {
"ProductDetails": [
{
"ProductDetail": {
"ProductSubDetails": {
"ProductAmount": 7.50,
"ProductReplenFrequency": "monthly",
"ProductsNotes": "Product Notes 1",
"pick": false
},
"ProductType": "PS4 Game"
},
"ProductSubType": "Game"
}
]
},
"ProductsCategoryId": "ProductS001",
"ProductSubType": "Computers and Games"
},
{
"Products": {
"ProductDetails": [
{
"ProductDetail": {
"ProductSubDetails": {
"ProductAmount": 7.50,
"ProductReplenFrequency": "fortnightly",
"ProductsNotes": "Products Notes 2",
"pick": false
},
"ProductType": "X Box One Game"
},
"ProductSubType": "Game"
}
]
},
"ProductsCategoryId": "ProductS001",
"ProductSubType": "Computers and Games"
}
]
Expected output:
[
{
"Products": {
"ProductDetails": [
{
"ProductDetail": {
"ProductSubDetails": {
"ProductAmount": 7.50,
"ProductReplenFrequency": "monthly",
"ProductsNotes": "Product Notes 1",
"pick": false
},
"ProductType": "PS4 Game"
},
"ProductSubType": "Game"
},
{
"ProductDetail": {
"ProductSubDetails": {
"ProductAmount": 7.50,
"ProductReplenFrequency": "fortnightly",
"ProductsNotes": "Products Notes 2",
"pick": false
},
"ProductType": "X Box One Game"
},
"ProductSubType": "Game"
}
]
},
"ProductsIdentifier": "ProductS001",
"ProductSubType": "Computers and Games"
}
]
Dataweave code:
%dw 1.0
%input payload application/csv
%output application/json skipNullOn = "everywhere" , encoding = "UTF-8"
%var dataLookup = {(payload."ProductsCategoryId" map {($.Id): $.Value})}
---
(payload filter $$ > 2) map ((payload01 , indexOfPayload01) -> {
Products: {
ProductsDetails: [{
ProductsDetail: {
ProductsubDetails: {
ProductsAmount: payload01."Products Amount" as :number,
ProductsFrequency: payload01."Products Frequency"
},
ProductsType: payload01."Products Type"
}
}]
},
ProductsCategoryId: payload01."ProductsCategoryId"
})
Sample Data:
ProductsCategoryId,Product Type,Product Frequency,Product Amount
ProductS001,PS4 Game,Monthly,7.5
ProductS001,X Box One Game,Fortnightly,7.5
ProductS002,Lego,Daily,7
Thanks