Reputation: 3
I am new to Mulesoft and working to create a flow where I am consuming a web service which gives XML as output and I need to do XML to JSON convert in Mule.
Below is my Mule flow,
Below is the Postman Pretty value,
<?xml version="1.0" encoding="UTF-8"?>
<GetProjectResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="namespaceName">
<GetProjectResult><root>
<header>
<status>
<message>Success</message>
</status>
</header>
<data>
<Project>
<ProjectId>12345</ProjectId>
<ProjectShortName>ABCDEF</ProjectShortName>
<BusinessGroupName>ABCDEF GROUP</BusinessGroupName>
<InstitutionShortName>Country</InstitutionShortName>
<CountryName>Sample</CountryName>
<Code>Sample</Code>
</Project>
</data>
</root></GetProjectResult>
</GetProjectResponse>
But my JSON looks like this ,
{
"GetProjectResponse": {
"@xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:xmlns": "namespaceName",
"GetProjectResult": "<root>\n <header>\n <status>\n <message>Success</message>\n </status>\n </header>\n <data>\n <Project>\n <ProjectId>12345</ProjectId>\n <ProjectShortName>ABCDEF</ProjectShortName>\n <BusinessGroupName>ABCDEF GROUP</BusinessGroupName>\n <InstitutionShortName>Country</InstitutionShortName>\n <CountryName>Sample</CountryName>\n <Code>Sample</Code>\n </Project>\n </data>\n</root>"
}
}
My actual XML is the value inside the "GetProjectResult" node, can someone tell me how I will achieve this.
Upvotes: 0
Views: 1521
Reputation: 11
this is because you XML is in string format so firstly convert it or use an XML file for it. then you can easily convert by transform message component.
Upvotes: 0
Reputation: 613
I used XSLT to obtain the 'GetProjectResult' object and then applied an in-built XmlToJson transformer.
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of disable-output-escaping="yes" select="//GetProjectResult" />
</xsl:template>
</xsl:stylesheet>
FLOW
XML FLOW:
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<mulexml:xslt-transformer xsl- file="test.xsl" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<json:xml-to-json-transformer doc:name="XML to JSON"/>
<logger message="#[payload:java.lang.String]" level="INFO" doc:name="Logger"/>
Json response after applying above flow is as below:
{
"root" : {
"header" : {
"status" : {
"message" : "Success"
}
},
"data" : {
"Project" : {
"ProjectId" : "12345",
"ProjectShortName" : "ABCDEF",
"BusinessGroupName" : "ABCDEF GROUP",
"InstitutionShortName" : "Country",
"CountryName" : "Sample",
"Code" : "Sample"
}
}
}
}
Is this your desired json ?
Upvotes: 0
Reputation: 1503
Option 3:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8083" basePath="test3" doc:name="HTTP Listener Configuration"/>
<flow name="removeFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<json:xml-to-json-transformer doc:name="XML to JSON"/>
<logger message="#[json:GetProjectResponse/GetProjectResult]" level="INFO" doc:name="Logger"/>
<set-variable variableName="extractJsondata" value="#[json:GetProjectResponse/GetProjectResult]" mimeType="application/json" doc:name="Variable"/>
<set-payload value="#[flowVars.extractJsondata]" doc:name="Set Payload"/>
<echo-component doc:name="Echo"/>
</flow>
I have given directly your xml as a input in my postman. Able to get the above mentioned result.
As an other way
json-to-object-transformer
and you can extract the payload.https://docs.mulesoft.com/mule-user-guide/v/3.7/mule-expression-language-tips ( Refer JSON Processing part in this url). Hope this helps.
Upvotes: 1
Reputation: 1503
3 options
1.You can use Xpath
or Xpath3
expression (based on Mule version) and extract the GetProjectResult
first and then you can convert in to Json using xmlToJson transformer which would be a simpler option.
2.Once the xml entered in to the flow removed the name space then converted in to Json
3.Use Directly xmlToJson transformer( Currently which you are doing), convert in to JSONtoObject, and extract the required value as mentioned in this link Extract values from a json response in mule flow (or) directly use Evaluate as mentioned in this link how to access json data mule esb
Upvotes: 0