DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Data Pipeline Techniques in Action
  • Tobiko Data: Revolutionizing Data Transformation With SQLMesh
  • Machine Learning With Python: Data Preprocessing Techniques
  • Financial Data Engineering in SAS

Trending

  • Building an Agentic Incident Resolution System for Developers
  • The Hidden Cost of AI-Generated Frontend Code
  • AI Is Finding Bugs Faster Than Enterprises Can Patch — Here's What Data Security Teams Should Do
  • How to Build a Local LLM Agent to Automate Work List Generation from Monthly Reports (With Jira Integration)
  1. DZone
  2. Data Engineering
  3. Data
  4. Data Transformation in Mule Using DataWeave

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.

By 
Rajesh Kumar user avatar
Rajesh Kumar
·
Jan. 23, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
25.0K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Data transformation Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Data Pipeline Techniques in Action
  • Tobiko Data: Revolutionizing Data Transformation With SQLMesh
  • Machine Learning With Python: Data Preprocessing Techniques
  • Financial Data Engineering in SAS

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook