简体   繁体   中英

Mule 4 DataWeave 2 Mapping of Arrays

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!

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM