-
using
: For initializing local variables in a DataWeave script. -
Unary DataWeave operators at this level:
-
.^
: Schema selector. -
.#
: Namespace selector. -
..
: Descendants selector. -
not
: Logical operator for negation. -
.@
: All attribute selector, for example, in a case that uses the expressionpayload.root.a.@
to return the attributes and values of the input payload<root> <a attr1="foo" attr2="bar" /></root>
within the array of objects{ "attr1": "foo", "attr2": "bar" }
.
-
Precedence in DataWeave
DataWeave expressions are compiled in a specific order. The result of a compilation of something at one level can serve as input for expressions in higher levels, but not at lower levels. Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For DataWeave in Mule 3 apps, refer to the DataWeave version 1.2 documentation. For other Mule versions, you can use the version selector in the DataWeave table of contents.
Order of Compilation
The following table orders operators and functions from first compiled (1) to last compiled (10):
Level | Operator or Function |
---|---|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
Order of Chained Function Calls
When functions are chained together in a series, the functions are processed in order from the first to the last function specified in the chain.
For example, the following script acts on the input array of objects defined
by the variable flights
. The script first calls filter
to return an array
that accepts all price
values that are less than 500. Then it calls
orderBy
to return an array that reorders the objects found in the input
from the lowest to the highest price
value. Finally, it calls groupBy
to group the input array of objects alphabetically by toAirport
value.
%dw 2.0
output application/json
var flights = [
{ "toAirport": "SFO", "price": 550, "airline": "American" },
{ "toAirport": "MUA", "price": 200, "airline": "American" },
{ "toAirport": "SFO", "price": 300, "airline": "American" },
{ "toAirport": "CLE", "price": 600, "airline": "American" },
{ "toAirport": "CLE", "price": 190, "airline": "American" },
{ "toAirport": "SFO", "price": 400, "airline": "American" }
]
---
flights filter $.price < 500 orderBy $.price groupBy $.toAirport
The result is an object that contains a collection of key-value pairs:
{ "CLE": [ { "toAirport": "CLE", "price": 190, "airline": "American" } ], "SFO": [ { "toAirport": "SFO", "price": 300, "airline": "United" }, { "toAirport": "SFO", "price": 400, "airline": "American" } ], "MUA": [ { "toAirport": "MUA", "price": 200, "airline": "American" } ] }
You can force the evaluation order by inserting evaluation parentheses (
and )
into the DataWeave expression. This example returns the same output as the previous example:
%dw 2.0
output application/json
var flights = [
{ "toAirport": "SFO", "price": 550, "airline": "American" },
{ "toAirport": "MUA", "price": 200, "airline": "American" },
{ "toAirport": "SFO", "price": 300, "airline": "American" },
{ "toAirport": "CLE", "price": 600, "airline": "American" },
{ "toAirport": "CLE", "price": 190, "airline": "American" },
{ "toAirport": "SFO", "price": 400, "airline": "American" }
]
---
( ( ( flights filter $.price < 500 ) orderBy $.price ) groupBy $.toAirport )
The result is an object that contains a collection of key-value pairs:
{ "CLE": [ { "toAirport": "CLE", "price": 190, "airline": "American" } ], "SFO": [ { "toAirport": "SFO", "price": 300, "airline": "United" }, { "toAirport": "SFO", "price": 400, "airline": "American" } ], "MUA": [ { "toAirport": "MUA", "price": 200, "airline": "American" } ] }
The order of the function calls is important. For example, the rearranged
expression flights groupBy $.toAirport filter $.price < 500 orderBy $.price
returns an error because groupBy
returns an object, and filter
expects an array. The resulting error is
Expecting Type: Array>T<, but got: {String: Array<{|toA… airline: String|}>}
.