Contact Us 1-800-596-4880

DataWeave Selectors

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

DataWeave 2 selectors traverse the structures of objects and arrays and return matching values.

A selector always operates within a context, which can be a reference to a variable, an object literal, an array literal, or the invocation of a DataWeave function.

For examples that use the selectors described below, see Extract Data.

Selector Type Syntax Return Type

Single-value

.keyName

Any type of value that belongs to a matching key

Multi-value

.*keyName

Array of values of any matching keys

Descendants

..keyName

Array of values of any matching descendant keys

Key-value pair

.&keyName

Object with the matching key

Index

[<index>]

Value of any type at selected array index

Range

[<index> to <index>]

Array with values from selected indexes

XML attribute

@, .@keyName

String value of the selected attribute

Namespace

keyName.#

String value of the namespace for the selected key

Key present

keyName?, keyName.@type?

Boolean (true if the selected key of an object or XML attribute is present, false if not)

Assert present

keyName!

String: Exception message if the key is not present

Filter

keyName[?(boolean_expression)]

Array or object containing key-value pairs if the DataWeave expression returns true. Otherwise, returns null.

Metadata

.^someMetadata

Returns the value of specified metadata for a Mule payload, variable, or attribute. The selector can return the value of class (.^class), encoding (.^encoding), mime type (.^mimeType), media type (.^mediaType), raw (.^raw), and custom (.^myCustomMetadata) metadata. For details, see Extract Data.

Use of Selectors on Content Stored in Variables

All selectors work with the predefined Mule Runtime variables, such as payload and attributes, and with DataWeave variables. For example, assuming a DataWeave variable defined as var myVar = { "id" : "1234", "name" : "somebody" }, the DataWeave expression myVar.name returns the value of "name", which is "somebody".

You can select Mule event data by using Mule Runtime variables.

Extracted values are handled as a literal values (as opposed to variables, for example) of one of the supported DataWeave value types.

Data to extract Syntax

Payload

payload, for example: payload.name

If the payload is {"name" : "somebody"}, payload.name returns "somebody".

For more on the Mule payload, see Message Payload.

Attribute

attributes.<myAttributeName>

For examples, see Attributes.

Variable

<myVariableName>

To avoid name collisions, you can prepend variables:

variables.<myVariableName>

For more on Mule variables, see Variables in Mule Apps.

Error object

error

For information on errors in the flow, you can use #[error.cause].

Flow

flow

For the flow name in the Logger: #[flow.name]

Note that flow.name does not work in some Core components, such as Set Payload and Transform Message.

For more on flows, see Flows and Subflows.