MuleSoft DataWeave Basics
DataWeave is MuleSoft's purpose-built language for transforming data. It reads data in one format and outputs it in another format. DataWeave is not just a template — it is a full functional programming language designed specifically for data transformation. Once you master DataWeave, you can handle any data transformation task that comes your way.
Why DataWeave Exists
Different systems speak different languages. A REST API speaks JSON. A legacy system speaks XML. A bank speaks flat files with fixed-width columns. DataWeave is the universal translator. You write a DataWeave script that reads one format and produces another, and MuleSoft executes it in milliseconds.
DataWeave as Universal Translator
Incoming Data (JSON from REST API):
{
"firstName": "Alice",
"lastName": "Smith",
"orderAmt": 250.00
}
DataWeave Script transforms it to XML for SAP:
<Customer>
<FullName>Alice Smith</FullName>
<Amount currency="USD">250.00</Amount>
</Customer>
Where You Write DataWeave
DataWeave code lives inside the Transform Message component in Anypoint Studio. Click on the Transform Message component on the canvas, and the DataWeave editor opens in the Properties Panel. The editor has two panes: the input on the left and the output on the right. You write your DataWeave script in the center.
DataWeave Script Structure
Every DataWeave script has two sections separated by three hyphens (---):
- Header: Declares the output format and imports any modules you need.
- Body: The transformation logic that produces the output.
DataWeave Script Layout
%dw 2.0 // DataWeave version
output application/json // Output format declaration
--- // Separator between header and body
{ // Body starts here
"fullName": payload.firstName ++ " " ++ payload.lastName,
"totalAmount": payload.orderAmt
}
Output Formats
DataWeave can produce output in many formats. Change the output line in the header to switch formats:
output application/json— JSON formatoutput application/xml— XML formatoutput application/csv— CSV (comma-separated values)output application/java— Java objects (used internally)output text/plain— Plain textoutput application/xlsx— Excel spreadsheet
Basic Transformations
Renaming Fields
Input (payload):
{ "fn": "Alice", "ln": "Smith", "amt": 250 }
DataWeave:
%dw 2.0
output application/json
---
{
"firstName": payload.fn,
"lastName": payload.ln,
"amount": payload.amt
}
Output:
{ "firstName": "Alice", "lastName": "Smith", "amount": 250 }
Combining Fields
Input (payload):
{ "firstName": "Alice", "lastName": "Smith" }
DataWeave:
%dw 2.0
output application/json
---
{
"fullName": payload.firstName ++ " " ++ payload.lastName
}
Output:
{ "fullName": "Alice Smith" }
Adding Calculated Fields
Input (payload):
{ "price": 100, "taxRate": 0.08 }
DataWeave:
%dw 2.0
output application/json
---
{
"price": payload.price,
"tax": payload.price * payload.taxRate,
"total": payload.price * (1 + payload.taxRate)
}
Output:
{ "price": 100, "tax": 8.0, "total": 108.0 }
Working with Arrays
The map function transforms each element of an array. It is the most commonly used DataWeave function.
map Function Example
Input (payload):
[
{ "name": "Pen", "price": 1.50 },
{ "name": "Book", "price": 12.00 },
{ "name": "Ruler", "price": 2.00 }
]
DataWeave:
%dw 2.0
output application/json
---
payload map (item) -> {
"product": item.name,
"cost": item.price,
"costWithTax": item.price * 1.1
}
Output:
[
{ "product": "Pen", "cost": 1.50, "costWithTax": 1.65 },
{ "product": "Book", "cost": 12.00, "costWithTax": 13.20 },
{ "product": "Ruler", "cost": 2.00, "costWithTax": 2.20 }
]
Conditional Logic with if/else
Input:
{ "score": 85 }
DataWeave:
%dw 2.0
output application/json
---
{
"score": payload.score,
"grade": if (payload.score >= 90) "A"
else if (payload.score >= 80) "B"
else if (payload.score >= 70) "C"
else "F"
}
Output:
{ "score": 85, "grade": "B" }
Handling Null Values
Use the default keyword to provide a fallback value when a field might be null or missing.
Input:
{ "name": "Alice", "phone": null }
DataWeave:
%dw 2.0
output application/json
---
{
"name": payload.name,
"phone": payload.phone default "Not provided"
}
Output:
{ "name": "Alice", "phone": "Not provided" }
JSON to XML Transformation
Input (JSON payload):
{ "customer": { "id": "CUST-001", "name": "Alice" } }
DataWeave:
%dw 2.0
output application/xml
---
{
Customer: {
Id: payload.customer.id,
Name: payload.customer.name
}
}
Output (XML):
<Customer>
<Id>CUST-001</Id>
<Name>Alice</Name>
</Customer>
Using Variables Inside DataWeave
You can access flow variables inside a DataWeave script using vars.variableName.
Flow variable set earlier:
vars.region = "US"
DataWeave:
%dw 2.0
output application/json
---
{
"orderId": payload.id,
"region": vars.region,
"currency": if (vars.region == "US") "USD" else "EUR"
}
Testing DataWeave Scripts
Anypoint Studio has a DataWeave Playground where you paste sample input data and run your script to see the output immediately without deploying the application. Use the Playground to test transformations quickly before wiring them into a flow. Right-click on a Transform Message component and select Preview to open the interactive DataWeave preview pane.
