Dataweave Transformation (Flat File to JSON) in Mule
Flat File is any text file containing data in string form.data of flat file will be converted into json form by Transform Message component.
Flat file to JSON flow :
All files are stored in src/main/resources
Input file connector :
General settings :
Connector Configuration :
Transform Message configuration :
for input meta data we have to choose type as Flat file and select flat file schema file input.ffd which is stored in src/main/resources
input.ffd :
form: FIXEDWIDTH
name: 'input'
values:
- { name: 'id', usage: M, type: Integer, length: 1 }
- { name: 'name', usage: M, type: String, length: 10 }
- { name: 'city', usage: M, type: String, length: 6 }
- { name: 'country', usage: M, type: String, length: 5 }
for output meta data choose output.json from src/main/resources
output.json :
{
"id" : 1,
"name" : "Himanshu",
"city" : "Pune",
"country": "india"
}
Connect input meta data with output meta data
Output file connector configuration :
General :
Connector Configuration :
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="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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
<file:connector name="File" outputPattern="inputfile.txt" 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_flatfiletojsonFlow">
<file:inbound-endpoint path="src/main/resources" connector-ref="File" responseTimeout="10000" doc:name="File"/>
<dw:transform-message doc:name="Transform Message" metadata:id="69301adc-599e-4db9-a0e6-e2cd570da0d5">
<dw:input-payload mimeType="text/plain">
<dw:reader-property name="schemaPath" value="input.ffd"/>
<dw:reader-property name="segmentIdent"/>
</dw:input-payload>
<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>
<file:outbound-endpoint path="src/main/resources" outputPattern="outputfile.json" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
No comments:
Post a Comment