DataWeave in MuleSoft: A Complete Guide with Examples

DataWeave in MuleSoft complete guide with JSON to XML example
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:
  1. Data transformation: convert between formats such as JSON, XML and CSV.
  2. Data mapping: map fields from the source to the required output structure.
  3. Data filtering: select only the information the target application needs.
  4. Data manipulation: modify values using expressions and built-in functions.
  5. 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:
  1. An application sends data to a Mule flow.
  2. MuleSoft receives the incoming message.
  3. The Transform Message component processes it using a DataWeave script.
  4. DataWeave maps or transforms the input into the required output format.
  5. The result is passed to the next component or the target application.
Example: an application sends customer data as JSON, but the receiving system needs XML. A DataWeave script reshapes that JSON payload into the XML format the target expects.

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

The map 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

  1. Open Anypoint Studio.
  2. Create or open a Mule project.
  3. Add a Transform Message component to the flow.
  4. Open the DataWeave editor.
  5. Enter a sample script.
  6. Provide sample input data.
  7. View the transformed result in the Preview pane.
  8. Modify the script and try different transformations.
Practising with JSON, XML, arrays, objects and functions will help you understand DataWeave faster.

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. Specify output 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.

Enroll For Free Demo

*By filling out the form you are giving us the consent to receive emails regarding all the updates.