In this guide
Modern businesses use many systems that exchange information, and those systems often store data in different formats such as JSON, XML and Java objects. For them to communicate, data must be transformed into a format the receiving application understands. This is where DataWeave in MuleSoft plays an important role.
DataWeave is MuleSoft’s primary data transformation language. It helps developers read, transform, map and generate data between formats. In this guide you will learn what DataWeave is, how it works, its key features, and practical examples for beginners.
What Is DataWeave in MuleSoft?
DataWeave is a functional programming language used in Mule applications to transform data. Read more in the official DataWeave documentation. For example, you can convert a JSON input into XML output, or map fields from one data structure to another. It is commonly used in the Transform Message component, where you write transformation scripts and preview the results. Supported formats include:- JSON
- XML
- CSV
- Java objects
- Plain text
Why Is DataWeave Important in MuleSoft?
Different applications use different data structures, which makes direct communication difficult. DataWeave gives you one consistent way to transform and map information. Key benefits:- Data transformation: convert between formats such as JSON, XML and CSV.
- Data mapping: map fields from the source to the required output structure.
- Data filtering: select only the information the target application needs.
- Data manipulation: modify values using expressions and built-in functions.
- Reusable logic: create functions and modules you can reuse across transformations.
How Does DataWeave Work in MuleSoft?
DataWeave in MuleSoft works on Mule messages and is usually used through the Transform Message component in Anypoint Studio. A typical flow:- An application sends data to a Mule flow.
- MuleSoft receives the incoming message.
- The Transform Message component processes it using a DataWeave script.
- DataWeave maps or transforms the input into the required output format.
- The result is passed to the next component or the target application.
Understanding DataWeave Script Structure
A basic DataWeave 2.0 script has two sections: the header (directives such as version, output format, imports, variables and functions) and the body (the expression that produces the output). A line of three dashes (---) divides the two.
%dw 2.0
output application/json
---
payload
%dw 2.0– the DataWeave version.output application/json– the output format.---– separates header from body.payload– returns the incoming message payload.
DataWeave Transformation Examples
Example 1: Transform JSON into XML
Input (JSON)
{
"id": 101,
"name": "Aravind",
"city": "Hyderabad"
}
DataWeave script
%dw 2.0
output application/xml
---
{
user: {
id: payload.id,
name: payload.name,
city: payload.city
}
}
Output (XML)
<?xml version='1.0' encoding='UTF-8'?>
<user>
<id>101</id>
<name>Aravind</name>
<city>Hyderabad</city>
</user>
DataWeave reads the JSON fields and builds a structured XML output.
Example 2: Transform XML into JSON
Input (XML)
<employee>
<id>101</id>
<name>Aravind</name>
<department>IT</department>
</employee>
DataWeave script
%dw 2.0
output application/json
---
{
employee: {
id: payload.employee.id,
name: payload.employee.name,
department: payload.employee.department
}
}
Output (JSON)
{
"employee": {
"id": "101",
"name": "Aravind",
"department": "IT"
}
}
The XML reader exposes elements through the payload structure, and DataWeave maps them into JSON fields. Note that id is a string ("101") because XML values are read as text.
Example 3: Using the map Function
Themap function processes each item in an array and creates a new array containing the transformed values.
Input (JSON)
[
{ "name": "Aravind", "city": "Hyderabad" },
{ "name": "Rahul", "city": "Bangalore" }
]
DataWeave script
%dw 2.0
output application/json
---
payload map (item) -> {
employeeName: item.name,
location: item.city
}
Output (JSON)
[
{ "employeeName": "Aravind", "location": "Hyderabad" },
{ "employeeName": "Rahul", "location": "Bangalore" }
]
Example 4: Variables, Functions, filter and Conditions Together
This example combines several features: a variable, a reusable function,filter and an if / else condition.
Input (JSON)
{ "users": [
{ "first": "Aravind", "last": "Reddy", "age": 25 },
{ "first": "Sita", "last": "Rao", "age": 65 },
{ "first": "Ravi", "last": "Kumar", "age": 15 }
] }
DataWeave script
%dw 2.0
output application/json
var minAge = 18
fun fullName(first, last) = first ++ " " ++ last
---
payload.users
filter ((u) -> u.age >= minAge)
map ((u) -> {
name: fullName(u.first, u.last),
category: if (u.age >= 60) "Senior" else "Adult"
})
Output (JSON)
[
{ "name": "Aravind Reddy", "category": "Adult" },
{ "name": "Sita Rao", "category": "Senior" }
]
Ravi is removed by the filter because he is under 18.
Important DataWeave Features
- Variables: store values and reuse them within a script.
- Functions: organise reusable transformation logic.
- Operators: mathematical, comparison and logical operations.
- Selectors: access specific fields and values in objects and arrays.
- Conditional expressions: produce different outputs based on conditions.
Where Is DataWeave Used?
- API integration: transform data exchanged between APIs.
- Database integration: map database records to the required structure.
- Enterprise application integration: exchange information between business applications.
- Data migration: convert data from one structure or format to another.
- Cloud integration: transform data exchanged between cloud services.
How to Practise DataWeave in MuleSoft Using Anypoint Studio
- Open Anypoint Studio.
- Create or open a Mule project.
- Add a Transform Message component to the flow.
- Open the DataWeave editor.
- Enter a sample script.
- Provide sample input data.
- View the transformed result in the Preview pane.
- Modify the script and try different transformations.
Frequently Asked Questions About DataWeave
1. What is DataWeave in MuleSoft?
MuleSoft’s primary data transformation language, used to transform and map data between formats in Mule applications.2. Is DataWeave a programming language?
Yes. It is a functional programming language designed for data transformation and mapping.3. What is DataWeave used for in MuleSoft?
To transform, filter, map and manipulate data exchanged between applications and systems.4. What is the Transform Message component?
A MuleSoft component that provides an editor for writing and testing DataWeave scripts.5. Can DataWeave convert JSON to XML?
Yes. Specifyoutput application/xml in the script.
6. What is the difference between DataWeave 1.0 and 2.0?
They are different language versions. DataWeave 2.0 is used with Mule 4; DataWeave 1.0 was used with Mule 3.Conclusion
DataWeave in MuleSoft is a core part of MuleSoft integration. It makes it easier to transform, map and manipulate data between systems and formats. By understanding its syntax, variables, functions, selectors and the examples above, beginners can build a strong foundation in MuleSoft development. Keep practising in Anypoint Studio with sample JSON and XML data.New to MuleSoft? Start with our beginner-friendly guide: What Is MuleSoft? A Beginner’s Guide to APIs & Integration.