Search code examples
dataweavemulesoft

Convert XML CDATA format into JSON format in dataweave 2.0 Mule4


Below is my sample XML response

<DATA><![CDATA[
<RESULT>0</RESULT>
<Addresses>
<Address>
 ...............
</Address>
<Address>
 ...............
</Address>
<Address>
 ...............
</Address>
</Addresses>

]]></DATA>

I want to transform this into a JSON array of objects. I used the below code, but it is just returning the CDATA string, not in JSON format

%dw 2.0
output application/json encoding="UTF-8"
---
payload

I'm using dataweave 2.0 in Mule 4


Solution

  • Complementing Salim's answer, you can try the following DataWeave expression in order to get access to the Addresses element:

    %dw 2.0
    output application/json
    ---
    read("<root>" ++ payload.DATA ++ "</root>", "application/xml").root.Addresses
    

    The idea is to wrap the DATA content with a root node, and then use it to access the inner elements.