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

Migrating from On-Prem to Cloud Middleware? Here’s what Aberdeen Group says leading companies should be considering. Brought to you in partnershp with Liaison Technologies

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.

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
mulesoft ,xml ,json ,integration ,conversions

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}