Request to please help me with the dataweave to achieve below output. Here we need to do the following.
I tried writing the dataweave for the same but was not able to achieve the desired response.
Request
<PacketResponse>
<PackResult>
<Result>0</Result>
<packs>
<pack>
<SeqNo>1</SeqNo>
<PackNumber>2550082245045</PackNumber>
<packageDate>2022-07-08T13:31:55.917+02:00</packageDate>
<Values i:nil="true"/>
</pack>
<pack>
<SeqNo>2</SeqNo>
<PackNumber>2550256260676</PackNumber>
<packageDate>2022-07-27T08:36:18.12+02:00</packageDate>
<Values>
<GetPackValue>
<Code>000228</Code>
<Count>80</Count>
<Amount>1600.00</Amount>
</GetPackValue>
<GetPackValue>
<Code>000227</Code>
<Count>20</Count>
<Amount>200.00</Amount>
</GetPackValue>
<GetPackValue>
<Code>000229</Code>
<Count>40</Count>
<Amount>2000.00</Amount>
</GetPackValue>
</Values>
</pack>
<pack>
<SeqNo>3</SeqNo>
<PackNumber>2550326072025</PackNumber>
<packageDate>2022-08-09T13:17:43.197+02:00</packageDate>
<Values>
<GetPackValue>
<Code>000240</Code>
<Count>5</Count>
<Amount>260.00</Amount>
</GetPackValue>
</Values>
</pack>
</packs>
</PackResult>
</PacketResponse>
Expected Response
[
{
"seqNo": "1",
"PackNumber": "2550082245045",
"packageDate": "2022-07-08 01:31:55",
"code": "000000",
"count": 0,
"amount": 0
},
{
"seqNo": "2",
"PackNumber": "2550256260676",
"code": "000228",
"packageDate": "2022-07-27 08:36:18",
"count": "80",
"amount": 1600.00
},
{
"seqNo": "2",
"PackNumber": "2550256260676",
"code": "000227",
"packageDate": "2022-07-27 08:36:18",
"count": "20",
"amount": 200.00
},
{
"seqNo": "2",
"PackNumber": "2550256260676",
"code": "000229",
"packageDate": "2022-07-27 08:36:18",
"count": "40",
"amount": 2000.00
},
{
"seqNo": "3",
"PackNumber": "2550326072025",
"code": "000240",
"packageDate": "2022-08-09 01:17:43",
"count": "5",
"amount": 260.00
}
]
I've used a combination of flatMap()
and multi-value selector:
%dw 2.0
output application/json
---
payload.PacketResponse.PackResult.packs.*pack flatMap ((pack, index) -> do {
var packResult = {
seqNo: pack.SeqNo,
"PackNumber": pack.PackNumber,
"packageDate": pack.packageDate,
}
---
pack.Values.*GetPackValue map ((packValue, index) -> packResult ++ packValue) default ([packResult ++ {"code": "000000", "count": 0,"amount": 0}])
}
)