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.