Reputation: 3
Can anyone please help with this dataweave? I am trying to map this input which is in "blocks" to split out into individual elements instead. I have following payload
[{
"invoiceId": [
9808,
9797
],
"items": [{
"order_item_id": [
25630,
25805]},
{
"qty": [
"1",
"1"
]
},
{
"extension_attributes": {
"credit_reason": [
"Cancelled",
"Unwanted Return"
]
}
}
],
"isOnline": true,
"notify": true,
"appendComment": true,
"comment": {
"comment": "<b>Reason:</b> Bulk Refund </br>"
},
"arguments": {
"shipping_amount": [
0,
0
],
"extension_attributes": {
"rd_creditmemo_payment_credited": 1,
"rd_send_email": 1
}
}
}
]
And I am trying to get to this output below which has the blocks split out into individual entries
[
{
"invoiceId": 9808,
"items": [
{
"order_item_id": 25630,
"qty": "1",
"extension_attributes": {
"credit_reason": "Cancelled"
}
}
],
"isOnline": false,
"notify": false,
"appendComment": true,
"comment": {
"comment": "<b>Reason:</b>Bulk Refunded</br>"
},
"arguments": {
"shipping_amount": 0,
"extension_attributes": {
"rd_credittmemo_payment_credited": 1,
"rd_send_email": 1
}
}
},
{
"invoiceId": 9797,
"items": [
{
"order_item_id": 25805,
"qty": "1",
"extension_attributes": {
"credit_reason": "Unwanted Return"
}
}
],
"isOnline": false,
"notify": false,
"appendComment": true,
"comment": {
"comment": "<b>Reason:</b>Bulk Refunded</br>"
},
"arguments": {
"shipping_amount": 4.95,
"extension_attributes": {
"rd_credittmemo_payment_credited": 1,
"rd_send_email": 1
}
}
}
]
Any help is much appreciated. Thank you in advance!
Upvotes: 0
Views: 265
Reputation: 628
Here is a way to achieve this.
%dw 2.0
output application/json
---
payload[0].invoiceId map ((invoiceId, index) -> {
invoiceId: invoiceId,
items: [
do {
var itemObj = payload[0].items
---
{
order_item_id: flatten(itemObj..order_item_id)[index],
qty: flatten(itemObj..qty)[index],
extension_attributes: {
credit_reason: flatten(itemObj..extension_attributes.credit_reason)[index]
}
}
}],
arguments: do {
var arg = payload[0].arguments
---
{
shipping_amount: arg.shipping_amount[index],
} ++ arg - "shipping_amount"
}
} ++ payload[0] - "invoiceId" - "items" - "arguments")
Upvotes: 1