Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Data Transformation in Mule Using DataWeave

DZone's Guide to

Data Transformation in Mule Using DataWeave

Data transformation is critical in software development and digitalization. DataWeave can be used in data transformation to convert CSV files to XML.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Data transformation is essential to day-to-day activities within an organization. It plays a critical role in digitalization.

DataWeave is the component provided by Mule for data transformation. In this article, I'm going to explain how to use it as part of Anypoint Studio, which is a GUI for building Mule flows from MuleSoft.

As part of the example, we will be converting the CSV file to an XML file using the DataWeave component. The DataWeave component is part of Anypoint Studio, so there is no need for separate installation.

MULE ESB 3.8 and Anypoint Studio 6.0 are used in this tutorial.

In this case, the input is a CSV file. Consider the below sample data:

ID Name Salary
1 Rajesh 25,000
2 Rohan 25,000
3 Mike 28,000
4 Charan 1,000
5 Ravi

30,000

The expected XML output from this CSV file is:

<?xml version='1.0' encoding='windows-1252'?>
<Root>
  <employees>
    <employee>
      <id> 1</id>
      <ContactName>rajesh</ContactName>
      <sal>25000</sal>
    </employee>
 <employee>
      <id> 2</id>
      <ContactName>rohan</ContactName>
      <sal>25000</sal>
    </employee>
 <employee>
      <id> 3</id>
      <ContactName>mike</ContactName>
      <sal>28000</sal>
    </employee>
 <employee>
      <id> 4</id>
      <ContactName>charan</ContactName>
      <sal>1000</sal>
    </employee>
 <employee>
      <id> 5</id>
      <ContactName>ravi</ContactName>
      <sal>30000</sal>
    </employee>

  </employees>
</Root>

First, use the file inbound connector to read the file:

<file:inbound-endpoint path="src/main/resources/input"
moveToDirectory="src/main/resources/processed" responseTimeout="10000"
metadata:id="3bfcea9d-6035-4f91-adbf-8eaaedeb1179" doc:name="File" />

Second, drag and drop the transformer from the design pallet to the flow:

<dw:transform-message metadata:id="710ab9d5-1327-4b95-baf0-275ec9d29e63" doc:name="Transform Message">

        </dw:transform-message>

Next, double click on the transformer component and set the metadata for input and output.

  • Input: Give the sample CSV file.

  • Output: Give the sample XML file.

For example:

<dw:transform-message metadata:id="710ab9d5-1327-4b95-baf0-275ec9d29e63" doc:name="Transform Message">
            <dw:input-payload doc:sample="sample_data\list_csv.csv" mimeType="application/csv"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
{
Root: {
employees: {
(payload map ((payload01 , indexOfPayload01) -> {
employee: {
id: payload01." 1",
Name: payload01.paul,
sal: payload01."25000",

}
}))
}
}
}]]></dw:set-payload>
        </dw:transform-message>

Now, use the file outbound connector to store the XML file.

<file:outbound-endpoint path="src/main/resources/output"
responseTimeout="10000" doc:name="File" outputPattern="#[function:systime].xml"/>

Here, I have given the XML name as time stamp '#[function:systime].xml'.

Execute the Mule application and check the console for the successful deployment.

Give the sample CSV in the input folder. Mule will pick up and create the required XML in the output folder.

Here's the total flow for reference:

<flow name="csv-to-xmlFlow">
    <file:inbound-endpoint path="src/main/resources/input"
moveToDirectory="src/main/resources/processed" responseTimeout="10000"
metadata:id="3bfcea9d-6035-4f91-adbf-8eaaedeb1179" doc:name="File" />
        <dw:transform-message metadata:id="710ab9d5-1327-4b95-baf0-275ec9d29e63" doc:name="Transform Message">
            <dw:input-payload doc:sample="sample_data\list_csv.csv" mimeType="application/csv"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
{
Root: {
employees: {
(payload map ((payload01 , indexOfPayload01) -> {
employee: {
id: payload01." 01",
name: payload01.paul,
sal: payload01."25000",

}
}))
}
}
}]]></dw:set-payload>
        </dw:transform-message>

<logger level="INFO" metadata:id="41c24045-cac4-470e-9778-d347a736494c"
doc:name="Logger" message="#[payload]"/>
<file:outbound-endpoint path="src/main/resources/output"
responseTimeout="10000" doc:name="File" outputPattern="#[function:systime].xml"/>
</flow>

You're done!

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
dataweave ,integration ,tutorial ,data transformation ,mule

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}