-
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 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended. 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 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 DataWeave version 2 is for Mule 4 apps. For a Mule 3 app, refer to the DataWeave 1.0 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.
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" } ] }
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|}>}
.