Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

XML to JSON Conversion in MuleSoft

DZone's Guide to

XML to JSON Conversion in MuleSoft

If you need to perform an XML to JSON conversion, MuleSoft is the way to go. It offers multiple ways to perform this transformation.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

MuleSoft supports XML to JSON transformations in multiple ways. Below are two similar ways to do this, along with solutions to overcome the problems in them.

1. Direct Conversion

Mule provides XML to JSON transformer activity that can directly convert an XML input into a JSON object. Below is the syntax that is generated when you drag an XML to JSON transformer from the Mule palette to the Mule flows.

<json:xml-to-json-transformer doc:name="XML to JSON"/>

The problem with the above approach is in dealing with repetitive elements inside the XML. This transformer doesn't really work when the repetitive XML element comes only once in the input payload. 

2. Conversion With DataWeave

Mule DataWeave also provides XML to JSON conversion in multiple ways. The easiest way to convert XML to JSON is to simply write PAYLOAD into DataWeave.

This approach also leads to a problem in dealing with repetitive elements inside the XML. Like in the first example, the transformer doesn't really work when the repetitive XML element comes only once in the input payload.  

The above problems can be resolved by defining both the input XML structure and the output JSON structure in the DataWeave transformation.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
mulesoft ,xml ,json ,integration ,conversions

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}