Dataweave Transformation (CSV to JSON) in Mule
We can use dataweave transformation to transform data from csv file into json format.
flow of csv to json transformation :
In above example input file connector reads the csv file from src/main/resources and send it to Transform message component.It transform the data from csv to json via dataweave language and send it to output file connector where file is saved in src/main/resources in json format.
all files are stored in src/main/resources.
inputfile.csv :
output.json file:
[{
"id" : "1",
"name" : "Himanshu",
"city" : "Pune",
"country" : "india"
}]
output.json is used for output metadata in Transform Message component.
Input file connector configuration :
General settings :
Connector configuration :
File Name Regex Filter :
Transform message configuration :
Input metadata configuration :
we have to set input metadata for type csv.
we have to set output metadata for type json.
Output file connector configuration :
General settings :
Connector Configuration :
import this code into your workspace for better understanding.
XML project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<file:connector name="File" writeToDirectory="src/main/resources" autoDelete="false" streaming="false" validateConnections="true" doc:name="File"/>
<file:connector name="File1" outputPattern="outputfile.json" writeToDirectory="src/main/resources" autoDelete="false" streaming="false" validateConnections="true" doc:name="File"/>
<flow name="test_dataweave_csvtojsonFlow">
<file:inbound-endpoint path="src/main/resources" connector-ref="File" responseTimeout="10000" doc:name="File" metadata:id="a0d01588-eb6f-4141-b537-44974ff91315">
<file:filename-regex-filter pattern="inputfile.csv" caseSensitive="true"/>
</file:inbound-endpoint>
<dw:transform-message doc:name="Transform Message" metadata:id="36c6171d-d20b-4ad1-96c5-a85f12cac4f5">
<dw:input-payload mimeType="application/csv"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload map ((payload01 , indexOfPayload01) -> {
id: payload01.ID,
name: payload01.NAME,
city: payload01.CITY,
country: payload01.COUNTRY
})]]></dw:set-payload>
</dw:transform-message>
<file:outbound-endpoint path="src/main/resources" outputPattern="outputfile.json" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
Thanks for your information. Very nice article keep posting.
ReplyDeleteonline mulesoft training
best mulesoft training