Dataweave Transformation (MAP to JSON) in Mule
MAP object to JSON transformation in dataweave is done by Transform Message component.
flow of MAP to JSON :
output.json is stored in src/main/resources
In input meta data choose Type as Map and add attributes as shown below :
for output metadata choose output.json from src/main/resources
output.json :
[{
"id" : "1",
"name" : "Himanshu",
"city" : "Pune",
"country" : "india"
}]
connect input meta data to output metadata
URL : http://localhost:8085/api/maptojson
Method : POST
Input :
Output :
XML project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:json="http://www.mulesoft.org/schema/mule/json" 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://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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_dataweavemaptojsonFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/maptojson" doc:name="HTTP"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<dw:transform-message doc:name="Transform Message" metadata:id="ffb9eba2-38ab-46d6-867e-09813c5c4039">
<dw:input-payload mimeType="application/java"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
[{
id: payload.id,
name: payload.name,
city: payload.city,
country: payload.country
}]]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>
No comments:
Post a Comment