简体   繁体   中英

Mule 4: map json to xml payload dataweave 2.0

I have a json payload in Mule that I am trying to convert to xml. Whatever value is in "entity_id" should be the "payment_id" in my output. Also whatever value comes for the key "selected_payment_option" should be "method" in my output.

Please see my sample input here:

{
    "items": [
           {
             "payment": {
               "entity_id": 1485222,
               "method": "m2_kp"
              },
              "extension_attributes": {
                "payment_addition_info":[
                    {
                        "key": "m1_code",
                        "value": "over_time"
                    },
                    {
                        "key": "order_id",
                        "value": "4f86-2cce-4870-ad05-089a333"
                    },
                    {
                        "key": "selected_payment_option",
                        "value": "slice_by_card"
                    }
                ]
             }
          }
   ]
}

And my desired output for this would be

<root>
 <payment>
 <payment_id>1485222</payment_id>
 <method>slice_by_card</method>
 </payment>
</root>

Any help would be greatly appreciated. Thank you!

  1. Used Map since items is an array hence there can be multiple entities inside items.
  2. Used default -> "" if key does not match selected_payment_option

DW

%dw 2.0
output application/xml
---
root:payment:payload.items map {
     payment_id: $.payment.entity_id,
     method: (flatten($..payment_addition_info) filter ($.key=="selected_payment_option"))[0].value default ""
}

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