1
votes

I am trying to convert the XML to JSON but not able to read following last currency attribute, how should present into json

><order id="2345">
<customer>
    <firstName>ronal</firstName>
    <lastName>kal</lastName>
    <address1>321 Main St.</address1>
    <address2>PO Box [526]</address2>
    <city>Atlanta</city>
    <state>IL</state>
    <zip>7562</zip>
    <orderTotal currency="USD">635.88</orderTotal>
</customer>

**

order:{
  orderid:payload.order.@id,
  customer:payload.order.*customer map 
  {
     Firstname:$.firstName,
     Lastname:$.lastName,
     Address1:$.address1,
     Address2:$.address2,
     City:$.city,
     State:$.state,
     zip:$.zip,
     Ordertoal:payload.
   }
2
An example output would also be helpful to show how you would like the attribute to be represented in the final payloadmaddestroyer7

2 Answers

4
votes

Is this what you are looking for ?

%dw 2.0
output application/json 
---
order:{
  orderid:payload.order.@id,
  customer:payload.order.*customer map
  {
     Firstname:$.firstName,
     Lastname:$.lastName,
     Address1:$.address1,
     Address2:$.address2,
     City:$.city,
     State:$.state,
     zip:$.zip,
     Ordertoal:$.orderTotal.@currency
  }
   }
0
votes

You can try this :

Input :

<order id="2345">
    <customer>
        <firstName>ronal</firstName>
        <lastName>kal</lastName>
        <address1>321 Main St.</address1>
        <address2>PO Box [526]</address2>
        <city>Atlanta</city>
        <state>IL</state>
        <zip>7562</zip>
        <orderTotal currency="USD">635.88</orderTotal>
    </customer>
    
</order>

Dataweave Code :

%dw 2.0
output application/json
---
order:{
  orderid:payload.order.@id,
  customer:payload.order.*customer map{
    "Firstname":$.firstName,
    "Lastname":$.lastName,
    "Address1":$.address1,
    "Address2":$.address2,
    "City":$.city,
    "State":$.state,
    "zip":$.zip,
    "OrderTotal":$.orderTotal ++ " " ++ ($.orderTotal.@currency)
  }
}
  

Output :

{
  "order": {
    "orderid": "2345",
    "customer": [
      {
        "Firstname": "ronal",
        "Lastname": "kal",
        "Address1": "321 Main St.",
        "Address2": "PO Box [526]",
        "City": "Atlanta",
        "State": "IL",
        "zip": "7562",
        "OrderTotal": "635.88 USD"
      }
    ]
  }
}

However for a clean code I prefer writing it this way :

%dw 2.0
output application/json
---
order:{
  orderid:payload.order.@id,
  customer:payload.order.*customer map ((item, index) ->{
    "Firstname":item.firstName,
    "Lastname":item.lastName,
    "Address1":item.address1,
    "Address2":item.address2,
    "City":item.city,
    "State":item.state,
    "zip":item.zip,
    "OrderTotal":item.orderTotal ++ " " ++ (item.orderTotal.@currency)
  } )
}