Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

DataWeave’s filter function selects values from an array when a predicate returns true. It passes the predicate both the current item and its index, and returns the matching original items without changing them.

DataWeave filter syntax and behavior

The current signature is filter<T>(items: Array<T>, criteria: (item: T, index: Number) -> Boolean): Array<T>. MuleSoft describes it as a function that “iterates over an array and applies an expression that returns matching values.” The predicate must return a Boolean for ordinary array filtering. If no item matches, the result is an empty array.

You can write filter in infix form after the array, or in prefix form with the collection as its first argument.

Infix form

[9, 2, 3, 4, 5] filter (value, index) -> value > 2

Result:

[9, 3, 4, 5]

Prefix form

Prefix form makes the input collection explicit, which can be useful when composing functions:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
filter(
  pluck(collaborators, (value, key, index) -> {
    Name: key,
    Role: upper(value.role),
    ID: value.id
  }),
  (item, index) -> item.Role == "ADMIN"
)

Here, pluck first creates an array of collaborator records; filter then retains the records whose Role is "ADMIN".

Filter records by a field

The predicate can inspect fields on each item, including nested fields. This example keeps records with an age of at least 30:

%dw 2.0
output application/json
---
[
  {name: "Mariano", age: 37},
  {name: "Shoki", age: 30},
  {name: "Tomo", age: 25},
  {name: "Ana", age: 29}
] filter ((value, index) -> value.age >= 30)

The result contains the Mariano and Shoki records. Their original fields and values are preserved; filter selects items but does not reshape them. Use map when you need to transform each value.

Use $ and $$ for the item and index

In a filter predicate, $ is shorthand for the current value and $$ is shorthand for its index. For example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
[9, 2, 3, 4, 5] filter (($$ > 1) and ($ < 5))

This keeps values below 5 only when their zero-based index is greater than 1, producing [3, 4]. Use named parameters such as (value, index) when they make a longer condition easier to read.

Choose the function for the input shape

filter is not the right operation for every input. Choose based on whether you are selecting array elements, string characters, object entries, or recursive tree values.

Input Construct Predicate inputs Result
Array filter Item and index Array of matching original items
String filter Character and index String of matching characters
Object filterObject Value, key, and index Object containing matching key-value pairs
Recursive tree filterTree Values and paths, as supported by the function Filtered tree structure

Filter string characters

The string overload keeps characters whose predicate is true. MuleSoft’s example filters "hello world" with "hello world" filter ($$ mod 2) == 0, retaining characters at even indexes to produce "hlowrd".

Filter object entries

Use filterObject when you want to retain key-value pairs in an object. Its predicate can inspect the value, key, and index. For example:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
{"a": "apple", "b": "banana"} filterObject ((value) -> value == "apple")

This retains the entry whose value is "apple".

Filter recursively with filterTree

Use filterTree when the task is to filter recursively through a tree by value or path, rather than just the immediate elements of one array. MuleSoft documents filterTree as introduced in DataWeave 2.4.0; check the runtime version when using it.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Filter functions versus filter selectors

Bracket filter selectors use the form [?(boolean_expression)]. They are a selector syntax for array or object contexts, distinct from calling the filter function. Use the function when you need its predicate-and-collection form, including composing it with other functions; use a selector when the bracket selector syntax fits the expression.

Handle null and no-match results

MuleSoft documents a helper overload that accepts Null and returns Null. That behavior is distinct from filtering a non-null array in which no elements match: that result is an empty array. For string input, no matching characters produce an empty string.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.