Pablo Estrada Pablo Estrada - 1 month ago 9
JSON Question

Mule: not converting XML to JSON conrrectly

I have a simple flow on AnyPointStudio that consumes a webservices and transforms it to JSON.

enter image description here

Im consuming a webservice that has the periodic table:
http://www.webservicex.net/periodictable.asmx?WSDL

enter image description here

However when using the getAtomicNumber service im getting the data of the xml as a string and the XML to JSON give this result:

{
"GetAtomicNumberResponse" : {
"@xmlns:xsd" : "http://www.w3.org/2001/XMLSchema",
"@xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:xmlns" : "http://www.webserviceX.NET",
"GetAtomicNumberResult" : "<NewDataSet>\n <Table>\n <AtomicNumber>49</AtomicNumber>\n <ElementName>Indium</ElementName>\n <Symbol>In</Symbol>\n <AtomicWeight>114.82000000000001</AtomicWeight>\n <BoilingPoint>2300</BoilingPoint>\n <IonisationPotential>5.79</IonisationPotential>\n <EletroNegativity>1.49</EletroNegativity>\n <AtomicRadius>1.5</AtomicRadius>\n <MeltingPoint>430</MeltingPoint>\n <Density>7310</Density>\n </Table>\n</NewDataSet>"
}
}


As you can see it does not parse the GetAtomicNumberResult.

How can I get the entire result parsed as JSON?

Answer

The problem you are facing is because the soap response object has an embedded object with the element details. What you would do is, extract the response object and perform xml to json after that. See below:

    <flow name="periodic_tableFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/table" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/xml
%namespace ns0 http://www.webserviceX.NET
---
{
       ns0#GetAtomicNumber: {
              ns0#ElementName: "Indium"
       }
}]]></dw:set-payload>
        </dw:transform-message>
        <logger message="#[message.payloadAs(String)]" level="INFO" doc:name="Logger"/>
        <ws:consumer config-ref="Web_Service_Consumer" operation="GetAtomicNumber" doc:name="Web Service Consumer"/>
        <dw:transform-message metadata:id="021335ff-1e33-4cdd-be35-49d95f93b8bc" doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
%namespace ns0 http://www.webserviceX.NET
---
payload.ns0#GetAtomicNumberResponse.ns0#GetAtomicNumberResult]]></dw:set-payload>
        </dw:transform-message>
        <json:xml-to-json-transformer doc:name="XML to JSON"/>
        <logger message="Before transformer - #[payload]" level="INFO" doc:name="Logger"/>
        <echo-component doc:name="Echo"/>
    </flow>
</mule>