I'm having difficulty in transforming a JSON array into a new structure whereby there is flat repeating keys, i.e Part and Labor, in this case.
[
{
"DisplayLineNumber": "1",
"LineDescription": "Bumper cover",
"Part": {
"PartTypeCode": "PAN",
"PriceColumn": "Standard",
"PartCategory": "PARTSTD"
},
"Labor": {
"LaborType": "LAB",
"DBLaborHours": "1.9",
"ActualLaborHours": "1.9",
"LaborAmt": "0.00"
},
"Labor": {
"LaborType": "LAR",
"DBLaborHours": "2.6",
"ActualLaborHours": "2.6",
"LaborAmt": "0.00"
}
},
{
"DisplayLineNumber": "2",
"LineAddedIndicator": "E01",
"LineReference": "1",
"SequenceNmbr": "3",
"Transaction": "1",
"LineDescription": "Add for Clear Coat",
"LineSource": "DATABASE",
"BettermentTaxedIndicator": "N",
"Labor": {
"LaborType": "LAR",
"DBLaborHours": "0.0",
"ActualLaborHours": "1.0",
"LaborAmt": "0.00"
},
"Part": {
"PartTypeCode": "PAN",
"PriceColumn": "Standard",
"PartCategory": "PARTSTD"
}
}
]
I'm trying to achieve a structure like the following below. An array of objects for each Labor or Part, that relate to the higher-level object whereby LineDescription is common.
[
{
"LineDescription": "Bumper cover",
"LaborOrPart" : "Part",
"PartTypeCode": "PAN",
"PriceColumn": "Standard",
"PartCategory": "PARTSTD"
},
{
"LineDescription": "Bumper cover",
"LaborOrPart" : "Labor",
"LaborType": "LAB",
"DBLaborHours": "1.9",
"ActualLaborHours": "1.9",
"LaborAmt": "0.00"
},
{
"LineDescription": "Bumper cover",
"LaborOrPart" : "Labor",
"LaborType": "LAR",
"DBLaborHours": "2.6",
"ActualLaborHours": "2.6",
"LaborAmt": "0.00"
},
{
"LineDescription": "Add for Clear Coat",
"LaborOrPart" : "Labor",
"LaborType": "LAR",
"DBLaborHours": "0.0",
"ActualLaborHours": "1.0",
"LaborAmt": "0.00"
},
{
"LineDescription": "Add for Clear Coat",
"LaborOrPart" : "Part",
"PartTypeCode": "PAN",
"PriceColumn": "Standard",
"PartCategory": "PARTSTD"
}
]
Any insight on how to achieve transforms like this using Dataweave 2.0 would be highly appreciated.
Thanks
CodePudding user response:
You can do it by plucking the keys in each element and adding the key to easily filter them, taking the line description from the original element and mapping all together. A nice trick is to add the key-values from Part or Labor keys directly, because if they are not present in an element they are ignored.
%dw 2.0
output application/json
var validKeys=["Part", "Labor"]
---
payload flatMap ((item, index) ->
(item pluck { key: $$, ($$):$ })
filter (validKeys contains $.key as String )
map {
LineDescription: item.LineDescription,
LaborOrPart: $.key,
($.Part),
($.Labor)
}
)
CodePudding user response:
I have used functions to reuse and flatMap for mapping.
%dw 2.0
output application/json
fun mapLineItem (items = [], itemType, lineDescription) = (
if (not isEmpty(items))
(
items map ( {
LineDescription: lineDescription,
LaborOrPart: itemType
} $)
)
else []
)
---
payload flatMap ((item) -> (
mapLineItem(item.*Part, "Part", item.LineDescription)
mapLineItem(item.*Labor, "Labor", item.LineDescription)
))