Home > Blockchain >  Mule 4: map json to xml payload dataweave 2.0
Mule 4: map json to xml payload dataweave 2.0

Time:02-20

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!

CodePudding user response:

I use the payment object from the input payload to generate the output. The root element needs to be added at the top:

%dw 2.0
output application/xml
---
{
    root: {
        payment: payload.items.payment
    }
}

CodePudding user response:

  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 ""
}
  • Related