Contact Free trial Login

Transform XML to JSON

This simple DataWeave example changes the XML input to JSON output by using selector expressions without any functions. It also changes the order and names of some of the fields. Note that more complex transformations usually require the use of the map or mapObject function.

The DataWeave script maps the names (or keys) for the output fields to values of the input fields. The input fields are specified with selector expressions.

%dw 2.0
output application/json
        address1: payload.order.buyer.address,
        country: payload.order.buyer.nationality,
        postalCode: payload.order.buyer.postCode,
        stateOrProvince: payload.order.buyer.state
Input XML
<?xml version='1.0' encoding='UTF-8'?>
    <model>MuleSoft Connect 2016</model>
    <address>Koala Boulevard 314</address>
    <city>San Diego</city>
  <shop>main branch</shop>
  <salesperson>Mathew Chow</salesperson>
Output JSON
  "address1": "Koala Boulevard 314",
  "city": "San Diego",
  "country": "USA",
  "email": "",
  "name": "Michael",
  "postalCode": "1345",
  "stateOrProvince": "CA"

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub