Dataweave Transformation (Fixed Width to JSON) in Mule
Fixed Width is basically a data format uses in Mainframe/AS 400 systems as a format of cobol language.its a string format in which attributes are of fix length.for example attribute name is defined of length 15 so we have to give name of length 15.if name completes on 10 then remaining 5 we have to give blank spaces,so after 15 length only new attribute can start.
Here we are converting fixed width or fixed length data to json format.
all files are located in src/main/resources :
Transform Message configuration :
For input metadata, select fixed width format from type list and define attributes as shown below :
This will generate fixed width schema input.ffd in src/main/resources
for output metadata, create a file output.json & define in output metadata
output.json :
{
"id" : 1,
"name" : "Himanshu",
"city" : "Pune"
}
connect input metadata to output metadata by dragging :
It's all set now !!
URL : http://localhost:8085/api/fixwidthtojson 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: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_dataweave_fixwidthtojsonFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/fixwidthtojson" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message" metadata:id="436d7a6a-2b9f-4d79-bf77-06c670e45979">
<dw:input-payload mimeType="text/plain">
<dw:reader-property name="schemaPath" value="input.ffd"/>
</dw:input-payload>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
id: payload[0].id,
name: payload[0].name,
city: payload[0].city
}]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>
No comments:
Post a Comment