MuleSoft DataWeave Operators

DataWeave operators are the tools you use to filter, sort, group, flatten, and combine data. These operators make complex data transformations straightforward. Every integration developer uses these operators daily, so mastering them saves significant development time.

filter Operator

The filter operator keeps only the array elements that match a condition. It removes everything else.

filter Example: Keep High-Value Orders

Input:
[
  { "orderId": "O1", "amount": 50  },
  { "orderId": "O2", "amount": 200 },
  { "orderId": "O3", "amount": 15  },
  { "orderId": "O4", "amount": 350 }
]

DataWeave:
%dw 2.0
output application/json
---
payload filter (order) -> order.amount > 100

Output:
[
  { "orderId": "O2", "amount": 200 },
  { "orderId": "O4", "amount": 350 }
]

map Operator

The map operator transforms every element in an array and returns a new array of the same size. It works like a factory line: each item goes in, gets processed, and comes out changed.

map Example: Format Products

Input:
[
  { "n": "Pen",  "p": 1.5 },
  { "n": "Book", "p": 12.0 }
]

DataWeave:
%dw 2.0
output application/json
---
payload map (item, index) -> {
  "position": index + 1,
  "name":     item.n,
  "price":    item.p,
  "priceTag": "USD " ++ item.p as String
}

Output:
[
  { "position": 1, "name": "Pen",  "price": 1.5,  "priceTag": "USD 1.5"  },
  { "position": 2, "name": "Book", "price": 12.0, "priceTag": "USD 12.0" }
]

reduce Operator

The reduce operator collapses an array into a single value. Use it to calculate totals, build strings, or merge data.

reduce Example: Total Order Value

Input:
[
  { "item": "Pen",  "price": 1.5  },
  { "item": "Book", "price": 12.0 },
  { "item": "Bag",  "price": 25.0 }
]

DataWeave:
%dw 2.0
output application/json
---
{
  "total": payload reduce ((item, acc = 0) -> acc + item.price)
}

Output:
{ "total": 38.5 }

groupBy Operator

The groupBy operator splits an array into groups based on a key. It returns an object where each key is a group name and each value is the list of matching items.

groupBy Example: Group Orders by Status

Input:
[
  { "id": 1, "status": "shipped",  "item": "Pen"   },
  { "id": 2, "status": "pending",  "item": "Book"  },
  { "id": 3, "status": "shipped",  "item": "Bag"   },
  { "id": 4, "status": "cancelled","item": "Ruler" },
  { "id": 5, "status": "pending",  "item": "Tape"  }
]

DataWeave:
%dw 2.0
output application/json
---
payload groupBy (order) -> order.status

Output:
{
  "shipped":   [ {id:1, status:"shipped",  item:"Pen"  },
                 {id:3, status:"shipped",  item:"Bag"  } ],
  "pending":   [ {id:2, status:"pending",  item:"Book" },
                 {id:5, status:"pending",  item:"Tape" } ],
  "cancelled": [ {id:4, status:"cancelled",item:"Ruler"} ]
}

orderBy Operator

The orderBy operator sorts an array by a field. Use it to sort products by price or records by date.

orderBy Example: Sort by Price

Input:
[
  { "name": "Bag",  "price": 25 },
  { "name": "Pen",  "price": 1  },
  { "name": "Book", "price": 12 }
]

DataWeave (ascending order):
%dw 2.0
output application/json
---
payload orderBy (item) -> item.price

Output:
[
  { "name": "Pen",  "price": 1  },
  { "name": "Book", "price": 12 },
  { "name": "Bag",  "price": 25 }
]

For descending order:
payload orderBy (item) -> -item.price

distinctBy Operator

The distinctBy operator removes duplicate entries from an array based on a key.

distinctBy Example: Remove Duplicate Customers

Input:
[
  { "email": "a@x.com", "name": "Alice" },
  { "email": "b@x.com", "name": "Bob"   },
  { "email": "a@x.com", "name": "Alice" }  // duplicate
]

DataWeave:
%dw 2.0
output application/json
---
payload distinctBy (c) -> c.email

Output:
[
  { "email": "a@x.com", "name": "Alice" },
  { "email": "b@x.com", "name": "Bob"   }
]

flatMap Operator

The flatMap operator transforms each element into an array and then flattens all results into a single array. Use it when each element contains a nested list that you want to pull up to the top level.

flatMap Example: Flatten Order Line Items

Input:
[
  { "order": "O1", "items": ["Pen", "Book"] },
  { "order": "O2", "items": ["Bag", "Ruler", "Tape"] }
]

DataWeave:
%dw 2.0
output application/json
---
payload flatMap (o) -> o.items map (i) -> { "order": o.order, "item": i }

Output:
[
  { "order": "O1", "item": "Pen"   },
  { "order": "O1", "item": "Book"  },
  { "order": "O2", "item": "Bag"   },
  { "order": "O2", "item": "Ruler" },
  { "order": "O2", "item": "Tape"  }
]

Chaining Operators

You can chain multiple operators together. Each operator feeds its output into the next. Read the chain from top to bottom like a pipeline.

Chained Operators: Filter, Sort, Map

Input:
[
  { "name": "Bag",  "price": 25, "inStock": true  },
  { "name": "Pen",  "price": 1,  "inStock": false },
  { "name": "Book", "price": 12, "inStock": true  },
  { "name": "Tape", "price": 3,  "inStock": true  }
]

Goal: Get only in-stock items, sorted by price, with a formatted label.

DataWeave:
%dw 2.0
output application/json
---
(payload 
  filter (i) -> i.inStock           // Step 1: keep in-stock only
  orderBy (i) -> i.price            // Step 2: sort by price ascending
) map (i) -> {                      // Step 3: format output
  "label": i.name ++ " - $" ++ (i.price as String)
}

Output:
[
  { "label": "Tape - $3"  },
  { "label": "Book - $12" },
  { "label": "Bag - $25"  }
]

The ++ Operator for Merging

The ++ operator concatenates strings and merges objects or arrays.

// Merge two objects:
{ "a": 1 } ++ { "b": 2 }
// Result: { "a": 1, "b": 2 }

// Merge two arrays:
[1, 2] ++ [3, 4]
// Result: [1, 2, 3, 4]

// Concatenate strings:
"Hello" ++ " " ++ "World"
// Result: "Hello World"

Practical Tip: Test Operators in the Preview Pane

In Anypoint Studio, open any Transform Message component and click Preview. Paste sample input data in the preview pane and run your DataWeave script. The output appears immediately. This lets you experiment with operators and see results without deploying the application.

Leave a Comment

Your email address will not be published. Required fields are marked *