DataWeave 2.0: Enterprise Data Transformations in MuleSoft
Master MuleSoft DataWeave 2.0 functional transformation language to manipulate XML, JSON, Flat Files, Java objects, and streaming collections efficiently.
Key Takeaways
- DataWeave 2.0 is a pure functional programming language built for high-performance in-memory and streaming data transformation in Mule 4
- Pattern matching (`match`), mapping (`map`, `mapObject`), and filtering (`filter`) enable expressive transformations without mutating variables
- DataWeave supports seamless cross-format conversions between JSON, XML, CSV, Java, and Fixed-Width formats in single expressions
The Diagnostic Context
In enterprise integration, data rarely arrives in the exact format required by the destination system. DataWeave 2.0 is MuleSoft’s purpose-built functional transformation engine that processes streaming inputs and outputs across JSON, XML, CSV, and Java with sub-millisecond execution speeds.
The Core Technique
DataWeave Header & Body Structure
Every DataWeave script consists of two sections separated by a delimiter (
---
- Header: Directives, input/output mime-types, custom functions, namespaces, and variables.
- Body: The expression generating the output structure.
%dw 2.0
output application/json
var exchangeRate = 1.08
fun formatCurrency(amount: Number, currency: String = "USD") =
currency ++ " " ++ (amount as String {format: "#,##0.00"})
---
{
orderId: payload.ORDER_HEADER.ID,
customer: {
fullName: payload.ORDER_HEADER.FIRST_NAME ++ " " ++ payload.ORDER_HEADER.LAST_NAME,
email: lower(payload.ORDER_HEADER.EMAIL default "")
},
items: payload.ORDER_HEADER.LINE_ITEMS map ((item, index) -> {
lineNumber: index + 1,
sku: item.PRODUCT_CODE,
quantity: item.QTY as Number,
unitPriceUSD: formatCurrency(item.PRICE_EUR * exchangeRate, "USD"),
isHighValue: (item.PRICE_EUR * item.QTY) > 500
}),
totalValueEUR: sum(payload.ORDER_HEADER.LINE_ITEMS.*PRICE_EUR)
}
Core DataWeave Operators & Functions
| Function / Operator | Purpose | Example | |---|---|---| |
map
payload.users map ((user) -> user.name)
mapObject
payload.attributes mapObject ((v, k) -> (upper(k)): v)
filter
payload.orders filter ((order) -> order.status == "PENDING")
groupBy
payload.transactions groupBy ((tx) -> tx.category)
match
status match { case "A" -> "ACTIVE" else -> "UNKNOWN" }default
null
payload.middleName default "N/A"
Streaming Large Payloads with CODE / PROMPTdeferred=true
deferred=true
For multi-gigabyte XML or CSV files, DataWeave streams data without loading entire files into JVM RAM:
%dw 2.0
output application/json deferred=true
---
payload map ((row) -> {
id: row.RECORD_ID,
timestamp: now()
})
Try This Right Now
Write a small DataWeave snippet that takes an array of employee JSON objects, filters for employees in the "Engineering" department, and outputs a CSV string with columns: `EmployeeID`, `FullName`, `SalaryBonus` (calculated as 15% of `baseSalary`).
Tip: Knowledge only becomes capability once you run the prompt yourself.