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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Concourse CI/CD Pipeline: Webhook Triggers
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  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
24.8K 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.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: