jnachi
Learning Hub
Enterprise Integration9 min readIntermediate

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.

Works with:DataWeave 2.0Anypoint StudioMule 4 Runtime

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 (

CODE / PROMPT
---
):

  1. Header: Directives, input/output mime-types, custom functions, namespaces, and variables.
  2. Body: The expression generating the output structure.
DATAWEAVE
%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 | |---|---|---| |

CODE / PROMPT
map
| Transforms array items into a new array |
CODE / PROMPT
payload.users map ((user) -> user.name)
| |
CODE / PROMPT
mapObject
| Iterates over key-value pairs of an object |
CODE / PROMPT
payload.attributes mapObject ((v, k) -> (upper(k)): v)
| |
CODE / PROMPT
filter
| Selects array elements matching a Boolean predicate |
CODE / PROMPT
payload.orders filter ((order) -> order.status == "PENDING")
| |
CODE / PROMPT
groupBy
| Groups array elements into object keys |
CODE / PROMPT
payload.transactions groupBy ((tx) -> tx.category)
| |
CODE / PROMPT
match
| Pattern matching on types, regex, or values |
CODE / PROMPT
status match { case "A" -> "ACTIVE" else -> "UNKNOWN" }
| |
CODE / PROMPT
default
| Provides fallback when value is
CODE / PROMPT
null
|
CODE / PROMPT
payload.middleName default "N/A"
|


Streaming Large Payloads with
CODE / PROMPT
deferred=true

For multi-gigabyte XML or CSV files, DataWeave streams data without loading entire files into JVM RAM:

DATAWEAVE
%dw 2.0
output application/json deferred=true
---
payload map ((row) -> {
  id: row.RECORD_ID,
  timestamp: now()
})
5-Minute Activation Challenge

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.

Comprehension Check

Test Your Instincts (3 Questions)

1

In DataWeave 2.0, which operator provides a safe fallback value when an input field is null or missing?

2

Which DataWeave functional operator is used to iterate over and transform the key-value pairs of a JSON Object (rather than an Array)?

3

How does DataWeave 2.0 handle memory consumption when processing large multi-gigabyte files?