Dataweave Conversion (String to JSON) in Mule
String to JSON conversion in dataweave is done by Transform Message component in Mule
flow of String to JSON :
output.json is stored in src/main/resources
for input meta data choose Type as Simple and String as shown below
for output metadata choose output.json from src/main/resources
output.json :
{
"id" : 1,
"name" : "Himanshu",
"city" : "Pune",
"country": "india"
}
URL : http://localhost:8085/api/simpletojson
Method : POST
Input :
Below input is in json format but type as string
Output :
Below output is in json form. you can check using any json validator.
XML project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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_dataweave_simpletojsonFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/simpletojson" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message" metadata:id="4799b01f-0a1b-4bb2-8230-3f9950ccea0d">
<dw:input-payload mimeType="application/java"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>
No comments:
Post a Comment