Selectors

The complex structure of Objects and Arrays can be navigated using Selector Expressions. Each selector expression returns either an object, an array, or a simple type. A selector always operates within a given context, which can be a reference to a variable, an object literal, an array literal, or the invocation of a function. As DataWeave processes a selector, a new context is set for further selectors, so you can navigate through the complex structures of arrays and objects by using chains of selectors, who’s depth is limited only by the depth of the current context.

There are 5 types of selector expression:

Single Value selector .<key-name>

Multi Value selector .*<key-name>

Descendants Selector ..<key-name>

Indexed Selector [<index>]

Attribute Selector .@<attribute-name>

Applying the Single level Explicit Selector, the Descendants Selector, or the Indexed Selector returns the value of the key:value pair that matches the expression.

Note: Each of these selector expressions supports having a '?' appended at the end of the chain. This changes the expression into a query that checks upon the existence of the key. The return type in this case is a boolean true or false.

Over :array

The selector is applied on each of the elements of the array that are of type :object and returns an array with all the selected values.

Alternative syntax

You can also select multiple values through the alternate syntax ["<key-name>"]. For example *payload["price"]* returns all the values whose key matches price. This is valid for both arrays and objects.

Transform

1
2
3
4

%dw 1.0
%output application/json
---
payload.items[*item]

Indexed Selector

The index selector returns the element at the specified position, it can be applied over an :array, an :object or a :string

Over :array

This selector can be applied to String literals, Arrays and Objects. In the case of Objects, the value of the key:value pair found at the index is returned. The index is zero based.

If the index is bigger or equal to 0, it starts counting from the beginning.

If the index is negative, it starts counting from the end where -1 is the last element.

Over :string

The Range selector limits the output to only the elements specified by the range on that specific order, treating the string as an array of characters. This selector allows you to slice a string or even invert it.

Descendants Selector

This selector is applied to the context using the form ..<field-name> and retrieves the values of all matching key:value pairs in the sub-tree under the current context. Regardless of the hierarchical structure these fields are organized in, they are all placed at the same level in the output.

Selecting all the Descendant Key Value Pairs

The as: object makes the expression return an object rather than an array (which would be returned by default). This implies that each value has a key. Without this conversion, in XML the returned array would be a single long string of characters comprised of all three names merged into one.

Selectors modifiers

There are two selectors modifiers: ? and !. The question mark returns true or false whether the keys are present on the structures. The exclamation mark evaluates the selection and fails if any key is not present.

Inbound Properties from a Mule Message

You can take Inbound Properties from the Mule message that arrives to the DataWeave transformer and use them in your transform body. To refer to one of these, simply call it through the matching Mule Expression Language (MEL) expression.

In MEL, there are two supported syntaxes to call for an inbound property:

inboundProperties.name

inboundProperties['name']

The first method only works if the variable name doesn’t include any periods or spaces.

1
2
3
4
5
6

%dw 1.0
%output application/xml
---
{
a: inboundProperties.userName
}

If the metadata about these inbound properties is known to Studio, an autocomplete function helps you out.

You can optionally also define the inbound property as a constant directive in the header, although this isn’t required as you can directly call the property as shown above.

%var inUname = inboundProperties['userName']

Outbound Properties from a Mule Message

You can take any Outbound Properties in the Mule message that arrives to the DataWeave transformer and use it in your transform body. To refer to it, simply call it through the matching Mule Expression Language (MEL) expression.

In MEL, there are two supported syntaxes to call an outbound property:

outboundProperties.name

outboundProperties['name']

The first method only works if the variable name doesn’t include any periods or spaces.

If the metadata about these outbound properties is known to Studio, an autocomplete function helps you out.

You can optionally also define the outbound property as a constant directive in the header, although this isn’t required as you can directly call the property as shown above.

%var outUname = outboundProperties['userName']

Flow Variables from a Mule Message

You can take any Flow Variable in the Mule message that arrives at the DataWeave transformer and use it in your transform body. To refer to it, simply call it through the matching Mule Expression Language (MEL) expression.

In MEL, there are two supported syntaxes to call a flow variable:

flowVars.name

flowVars['name']

The first method only works if the variable name doesn’t include any periods or spaces.

1
2
3
4
5
6

%dw 1.0
%output application/xml
---
{
a: flowVars.userName
}

If the metadata about these flow variables is known to Studio, an autocomplete function helps you out.

You can optionally also define the flow variable as a constant directive in the header, although this isn’t required as you can directly call the variable as shown above.

%var uname = flowVars['userName']

Accessing System and Spring Properties

You can reference any Property (System or Spring) that exists in the server while DataWeave is processing your transformation, to do so use the p('prop_name') function or '${prop_name}'.