JSON to XML Transformation Using DataWeave 2.0 in Mule 4.0
This post will explain the simple way to do a JSON to XML transformation in MuleSoft by using DataWeave.
Join the DZone community and get the full member experience.
Join For FreeNowadays, most of the data would be like JSON or XML as the input/output to every system. To get the required format for downstream system bit complex to transform but MuleSoft will do that easily and push into the target system. This post will explain the simple way to do a JSON to XML transformation in MuleSoft. This can be done by using DataWeave, and not only JSON, but others like Java, CSV, and XML format.
Step1: Select the Mule project from Anypoint Studio and write the project name. Then, click ok.
Step2: Drag and drop HTTP listener, and transform the message to track the logs logger component from Mule palette.
Step3: Load the metadata and map the data as shown in the below screenshot.
Step4: After that, you can deploy your application by right clicking on the flow and running the project databaseSelect. Then you will see the DEPLOYED status in the console.
Step5: Check that your application is running as expected from Postman.
Code snippet:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="8f99e5e7-3375-46e5-ad5e-23b6c4d9624c" >
<http:listener-connection host="0.0.0.0" port="6757" />
</http:listener-config>
<flow name="jsontoxmlFlow" doc:id="37c1e6ab-60f2-423e-9f95-a1c6417844da" >
<http:listener doc:name="Listener" doc:id="64cb15b9-06d1-4e9a-8b79-8adca4ca1892" config-ref="HTTP_Listener_config" path="/jsontoxml">
<ee:repeatable-file-store-stream />
</http:listener>
<ee:transform doc:name="Transform Message" doc:id="5cf58b0e-23ad-4925-a763-88bc7a906349" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/xml
---
{
test:payload
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="6f05b849-991d-4034-9e5d-4f6751fdd030" message="Success: #[payload] "/>
</flow>
</mule>
Thank you.
Opinions expressed by DZone contributors are their own.
Comments