0
votes

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

3

3 Answers

2
votes

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.

3
votes

Try with this script:

Input

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

]]></DATA>

Script

%dw 2.0
output application/json
---
read(payload.DATA, "application/xml")

Output

{
  "Person": {
    "RESULT": "0",
    "Addresses": {
      "Address": "\n ...............\n",
      "Address": "\n ...............\n",
      "Address": "\n ...............\n"
    }
  }
}
0
votes

I followed your directions and it worked. Transformer 1:

%dw 2.0 output application/json

read(payload.DATA, "application/xml")

Transformer 2:

%dw 2.0 output application/java

write(payload,'application/xml')

thanks dk