Reputation: 109
I have below payload with a hierarchy and trying to filter based on the columnn "DeleteIndicator" = "Y". These are the fixed columns I will be getting. I want to traverse through the payload and check for the DeleteIndicator. Updated the input and expected output for multiple child in inner array.
Input:
{
"Num":4363886,
"LineItems":[
{
"DetailGUID":"B439E023360C",
"DeleteIndicator":"Y"
},
{
"DetailGUID":"B439E023360C",
"LineQuantity":[
{
"AltGUID":"2B43AC4203DC",
"DeleteIndicator":"Y"
},
{
"AltGUID":"2B43AC4203DD",
"DeleteIndicator":"Y"
}
]
},
{
"DetailGUID":"B439E023360C",
"LineQuantity":[
{
"AltGUID":"2B43AC4203DC",
"ShipTo":[
{
"ShipToGUID":"2B43AC4201AB",
"DeleteIndicator":"Y"
},
{
"ShipToGUID":"2B43AC4201AC",
"DeleteIndicator":"Y"
}
]
}
]
}
]
}
Expected Output:
{
"Num":4363886,
"Details":[
{
"DetailGUID":"B439E023360C",
"AltGUID":null,
"ShipToGUID":null
},
{
"DetailGUID":"B439E023360C",
"AltGUID":"2B43AC4203DC",
"ShipToGUID":null
},
{
"DetailGUID":"B439E023360C",
"AltGUID":"2B43AC4203DD",
"ShipToGUID":null
},
{
"DetailGUID":"B439E023360C",
"AltGUID":"2B43AC4203DC",
"ShipToGUID":"2B43AC4201AB"
},
{
"DetailGUID":"B439E023360C",
"AltGUID":"2B43AC4203DC",
"ShipToGUID":"2B43AC4201AC"
}
]
}
Upvotes: 0
Views: 1566
Reputation: 3262
Well, it is a little complicated, but I have tried my best to make the code readable and documented. Try to look at the problem from a different angle. Think that you are trying to "spread" the fields of "LineItems" across all the "LineQuantity".
%dw 2.0
// This Function takes a json and an array and spreads the json fields
// across each elements of the array.
fun spreadAcrossArray(json: Object, array: Array | Null) = (array default [{}]) map {
(json),
($)
}
//Here is the main logic of getting the payload.
//The idea is first to "Spread" the lineItem Elements across all the "LineQuantity"
//and then spread those to all "ShipTo" elements.
fun spreadLineItemAcrossLineQuantity(lineItems: Array) = lineItems map ((lineItem) -> do {
var lineItemSpreadedAcrossQuantity =
{DetailGUID: lineItem.DetailGUID} spreadAcrossArray lineItem.LineQuantity
---
flatten(lineItemSpreadedAcrossQuantity map ({
DetailGUID: $.DetailGUID,
AltGUID: $.AltGUID
} spreadAcrossArray $.ShipTo)) //Map app the lineItemSpreadedAcrossQuantity with spreading them to lineItemSpreadedAcrossQuantity.ShipTo
}
)
output application/json
---
{
Num: payload.Num,
Details: flatten(spreadLineItemAcrossLineQuantity(payload.LineItems))
map {
DetailGUID: $.DetailGUID,
AltGUID: $.AltGUID,
ShipToGUID: $.ShipToGUID
}
}
It requires the array to be flattened later because you are basically passing an Object in the spreadAcrossArray
function and in return you are getting an array. And the map function is also returning an array and so you are getting an array of arrays.
Upvotes: 0
Reputation: 184
This should get your work done.Please let me know if you need any clarifications.
%dw 2.0
output application/json
---
{
Num:payload.Num,
Details:payload.LineItems map (v0,k0) ->
{
DetailGUID:v0..DetailGUID[0],
AltGUID:v0..AltGUID[0],
ShipToGUID:v0..ShipToGUID[0]
}
}
Upvotes: 0