Dataweave Transformation (JSON to XML) in Mule
JSON to XML in dataweave is done by Transform Message component.
If you want to convert JSON to XML online kindly visit: JSON to XML
flow of JSON to XML :
input.json and output.xml for metadata is stored in src/main/resources
for input metadata choose input.json from src/main/resources
input.json :
{
"id" : 1,
"name" : "Himanshu",
"city" : "Pune"
}
for output metadata choose output.xml from src/main/resources
output.xml :
<employee>
<id>1</id>
<name>Himanshu</name>
<city>pune</city>
</employee>
connect input meta data with output meta data
URL: http://localhost:8085/api/jsontoxml
Method: POST
Input :
Output :
XML project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" 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:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
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/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
<flow name="test_transformmessageFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/jsontoxml" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message" metadata:id="85e55429-e597-4f00-a3ea-0f6fff2f6706">
<dw:input-payload mimeType="application/json"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
{
employee: {
id: payload.id,
name: payload.name,
city: payload.city
}
}]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>
No comments:
Post a Comment