Mule: Convert XML to JSON with with some of the single element in XML as array in JSON
Unfortunately there is no direct way of telling the JSON XML to JSON transformer to treat the element as an Array.
There are two options:
1. Implement a custom JSON transformer of Jackson and use the Force Array option. Please refer to the Jackson webpages for more detials.2. If possible convert your XML to Java Object. The object will definitely would have defined the elements as an array or colelction.Then convert the object to JSON which would be the final expected result of sinngle element as Array in JSON. <mulexml:jaxb-xml-to-object-transformer name="XmlToObject" jaxbContext-ref="JAXBContext" returnClass="java.lang.Object"/> <json:object-to-json-transformer doc:name="Object to JSON"/>
Hope this helps.
here is a different approach using dataweave.
%dw 1.0%output application/json---{ team: { employee: payload.team.*employee default [] }}
result is a employee attribute, wich always contains a list.
for default []
to work you must specify the reader configuration like this:
<dw:transform-message metadata:id="06021518-b51d-4dbf-983d-b47ed5ffd0bc" doc:name="Transform Message"> <dw:input-payload doc:sample="team.xml" mimeType="application/xml"> <dw:reader-property name="nullValueOn" value="blank"/> </dw:input-payload> <dw:set-payload><!-- content of dataweave left out --></dw:set-payload></dw:transform-message>
I implemented this by adding an attribute to the element that needs to be treated as array and handling this by writing a custom Jackson transformer by extending the
Blockquote com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer .
Hope this helps anyone trying to achieve this.