Dataweave Conversion (EXCEL to JSON) in Mule
EXCEL to JSON in dataweave is done by Transform Message component.
flow of excel to json :
all files are stored in src/main/resources
input.xlsx :
input file connector configuration :
General settings :
connector configuration :
for input meta data choose Type as Excel and add attributes as shown below :
for output meta data choose output.json from src/main/resources
output.json :
[{
"id" : "1",
"name" : "Himanshu",
"city" : "Pune",
"country" : "india"
}]
connect input and output metadata
output file connector configuration :
General settings :
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://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" outputPattern="input.xlsx" writeToDirectory="src/main/resources" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
<file:connector name="File1" outputPattern="outputfile.json" writeToDirectory="src/main/resources" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
<flow name="test_dataweaveexceltojsonFlow">
<file:inbound-endpoint path="src/main/resources" connector-ref="File" responseTimeout="10000" doc:name="File"/>
<dw:transform-message doc:name="Transform Message" metadata:id="8444760a-95e7-418f-a7f7-bdd9c698ec51">
<dw:input-payload mimeType="application/xlsx"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload.Sheet1 map ((sheet1 , indexOfSheet1) -> {
id: sheet1.ID,
name: sheet1.NAME,
city: sheet1.CITY,
country: sheet1.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