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.

Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements. What's below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

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.

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
mulesoft ,xml ,json ,integration ,conversions

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}