Contact Free trial Login

DataWeave Selectors

DataWeave 2.0 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.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.