Returns the value of specified metadata for a Mule payload, variable, or
attribute. The selector can return the value of class (.^class
),
content length (.^contentLength
), encoding
(.^encoding
), mime type (.^mimeType
), media type (.^mediaType
),
raw (.^raw
), and custom (.^myCustomMetadata
) metadata. For details, see
Extract Data.
DataWeave Selectors
DataWeave version 2 selectors traverse the structures of objects and arrays and return matching values. Before you begin, note that DataWeave version 2 is for Mule 4 apps. For a Mule 3 app, refer to the DataWeave version 1 documentation set in the Mule 3.9 documentation. For other Mule versions, you can use the version selector for the Mule Runtime table of contents.
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.
Selector Type | Syntax | Return Type |
---|---|---|
Single-value |
|
Any type of value that belongs to a matching key |
Multi-value |
|
Array of values of any matching keys |
Descendants |
|
Array of values of any matching descendant keys |
Dynamic |
See Dynamic Selector. |
|
Key-value pair |
|
Object with the matching key |
Index |
|
Value of any type at selected array index. Use negative numbers to index from the end of an array, for example, |
Range |
|
Array with values from selected indexes |
XML attribute |
|
String value of the selected attribute |
Namespace |
|
String value of the namespace for the selected key |
Key present |
|
Boolean ( |
Assert present |
|
String: Exception message if the key is not present |
Filter |
|
Array or object
containing key-value pairs if the DataWeave expression returns |
Metadata |
|
The following examples use these selectors. For additional examples, see Extract Data. |
Rules for Matching
In DataWeave, a name that matches the key of an object (such as the key
"someName"
in the JSON object { "someName" : "Data Weave" }
) can select
data from that object. For a simple example, see Single-Value Selector.
Single-Value Selector
This selector matches the value of the first key-value pair in which the key
matches the given selector name. The selector can be applied to an Object or
to an array. On an array, the selector applies to all Object values inside the
array and ignores all values that are not Object values. If no key matches,
the value null
is returned.
Example: Using a Single-Value Selector on an Object
This example shows how the selector works on a simple JSON object.
Input Payload
The input payload is an object with the key "name"
and the
value "DataWeave"
:
{ "name": "Data Weave" }
Example: Using a Single-Value Selector on an Array
On an array, the selector applies to every element.
Input Payload
The input payload is an array that contains two objects with the same key, "name":
[
{
"name": "Arg"
},
{
"name": "Japan"
}
]
Namespace Selector
The DataWeave namespace selector supports the use of a name that matches a namespace or a local part. To match a namespace, the name must match the namespace and the local part. If the name only matches the local part, the selector matches all namespaces with that local part, regardless of the namespace.
Note that XML namespaces and their prefixes are defined in the xmlns
attribute
of an XML element. For example, the element
<h:table xmlns:h="http://www.w3.org/TR/html4/"/>
defines a namespace that
assigns the prefix h
to the namespace http://www.w3.org/TR/html4/
.
All elements in that namespace contain the h
prefix. For example, in the
element <h:table/>
, h
is the prefix for the namespace, and table
is the
local part.
Example: Using a Namespace Selector
This example selects values from XML elements using the namespace and local part.
Input Payload
The input payload contains XML elements, h:table
and f:table
, that have
different namespaces but the same local names (table
):
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="https://www.w3schools.com/furniture">
<f:tr>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:tr>
</f:table>
</root>
DataWeave Source
The DataWeave script selects XML content from specified namespaces. The
script’s header creates namespace (ns
) variables html
and furniture
to store namespaces from the input payload. To select the children of h:table
into element a
and the children of f:table
elements into element b
, the
script uses the html
and furniture
namespace variables with a
namespace selector (#
) and local part (table
):
%dw 2.0
output application/xml
ns html http://www.w3.org/TR/html4/
ns furniture https://www.w3schools.com/furniture
---
root: {
a: payload.root.html#table,
b: payload.root.furniture#table
}
Output
The script outputs children of the h:table
element under element a
and
the children of the f:table
under element b
:
<?xml version='1.0' encoding='UTF-8'?>
<root>
<a>
<h:tr xmlns:h="http://www.w3.org/TR/html4/">
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</a>
<b>
<f:tr xmlns:f="https://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:tr>
</b>
</root>
Example: Using a Local Name to Select XML Values
This example selects XML values using a local name table
from the h:table
element.
Input Payload
The input payload contains XML with a table
element that contains
the namespace "http://www.w3.org/TR/html4/"
:
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
</root>
Attribute Selector
The attribute selector returns the first attribute value that matches the
selected name expression. If no key matches, the selection returns the value
null
.
The following example selects the value of an XML attribute.
Input Payload
The input payload is an XML user
element that contains the attribute
name="Weave"
:
<user name="Weave"/>
Multi-Value Selector
Instead of returning the value of the first matching key in an array
of objects, the multi-value selector (*
) returns an array containing
values to the matching key (for example, *user
where user
is
the key). The selector does not return the values of descendants,
only those at the specified level. If no key matches, the selection
returns the value null
.
Input Payload
The input payload contains an XML array of user
elements.
<users>
<user>Weave</user>
<user>BAT</user>
<user>TF</user>
</users>
Descendant Selector
The descendant selector returns a list of all children and their descendants.
You can directly chain
this selector to any other selector without using a single .
. For example,
payload..
recursively returns an array of all the child values, the values
of their children, and so on. You can also chain the selector to another element
(for example, with payload..user
) to select the values of each user
key and
its descendants, or you can use payload..*name
to select the values
of all name
descendants.
Example: Selecting Each Descendant
This example selects each descendant of the input payload.
Input Payload
The input payload contains a set of elements that are nested at different levels.
<users>
<user>
<name>Weave</name>
<user>
<name>BAT</name>
<user>
<name>BDD</name>
</user>
</user>
</user>
</users>
Example: Selecting Descending Values
This example selects the descending name
values.
Input Payload
The input payload contains a set of name
elements that are nested
at different levels.
<users>
<user>
<name>Weave</name>
<user>
<name>BAT</name>
<name>Munit</name>
<user>
<name>BDD</name>
</user>
</user>
</user>
</users>
Example
This example selects the descending name
values.
Input Payload
The input payload contains a set of name
elements that are nested
at different levels.
<users>
<user>
<name>Weave</name>
<user>
<name>BAT</name>
<name>Munit</name>
<user>
<name>BDD</name>
</user>
</user>
</user>
</users>
DataWeave Source
The DataWeave script uses ..*
to select the values of all name
elements
from the input payload and output those values into a JSON array, including
the BAT
and Munit
values of the repeated name
keys that are at the same level in the XML hierarchy.
%dw 2.0
output application/json
---
payload..*name
Example: Selecting Descending Values
This example selects the descending Name
values of the Item
elements.
Input Payload
The input payload contains a set of Name
elements that are nested
at different levels.
<Example>
<Brand>
<Id>32345</Id>
<logo>circle</logo>
<Item>
<Name>Perfume</Name>
<Item>
<Name>Bosque</Name>
</Item>
</Item>
<Item>
<Name>t-Shirt</Name>
</Item>
</Brand>
<Brand>
<Id>435678C</Id>
<logo>circle</logo>
<Item>
<Name>t-Shirt2</Name>
<Item>
<Name>t-Shirt red</Name>
<Item>
<Name>t-Shirt red with logo</Name>
</Item>
</Item>
</Item>
</Brand>
</Example>
Dynamic Selector
The syntax for dynamic selection depends on what you are selecting:
-
Single Value:
payload[(nameExpression)]
-
Multi Value:
payload[*(nameExpression)]
-
Attribute:
payload[@(nameExpression)]
-
Key Value:
payload[&(nameExpression)]
-
Single Value with a Namespace:
payload.ns0#"$(nameExpression)"
Example: Dynamically Selecting a Single Value
This example shows how to dynamically select a single value.
Input Payload
The input payload is an array of objects. The first object has the key "ref"
and the value "name"
. The second has the key "name"
and the value
"Data Weave"
:
{ "ref": "name", "name": "Data Weave" }
DataWeave Source
The DataWeave script dynamically selects the value of the "name"
key:
%dw 2.0
output application/json
---
payload[(payload.ref)]
Notice that it passes payload.ref
within the parentheses of dynamic selector
[()]
. The script works because the value of "ref"
is "name"
, which matches
the key "name"
.
Example: Dynamically Selecting a Single Value with a Namespace
This example shows how to dynamically select a single value that contains a namespace.
Input Payload
The input payload contains a <root/>
element with two child elements, one of
which has the namespace http://www.w3.org/TR/html4/
. Both child elements
(<f:table/>
and <h:table>
) have the local name table
:
<root ref="table">
<f:table xmlns:f="https://www.w3schools.com/furniture">Manzana</f:table>
<h:table xmlns:h="http://www.w3.org/TR/html4/">Banana</h:table>
</root>
DataWeave Source
The DataWeave script dynamically selects the value of the element that has
the namespace http://www.w3.org/TR/html4/
:
%dw 2.0
output application/json
ns h http://www.w3.org/TR/html4/
---
payload.root.h#"$(payload.root.@ref)"
Notice that the expression payload.root.@ref
uses the attribute selector (@
)
on the ref
attribute of the root
element to select the value table
,
which matches local name table
in the element
<h:table xmlns:h="http://www.w3.org/TR/html4/">Banana</h:table>
.
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 |
If the For more on the Mule payload, see Message Payload. |
Attribute |
For examples, see Attributes. |
Variable |
To avoid name collisions, you can prepend
For more on Mule variables, see Variables in Mule Apps. |
Error object |
For information on errors in the flow, you can use |
Flow |
For the flow name in the Logger: Note that For more on flows, see Flows and Subflows. |