Contact Us 1-800-596-4880

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

  • 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 expression payload.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" }.

2

  • as: Used for type coercion.

3

  • *: Multiply.

  • /: Divide.

4

  • +: Add.

  • -: Subtract.

  • >>: Prepend data.

  • <<: Append data.

5

  • >: Greater than or equal to.

  • =: Equal to.

  • <: Less than or equal to.

  • <: Less than.

  • >: Greater than.

  • is: Compare type, for example, used in Remove Specified XML Attributes.

6

  • !=: Not equal to.

  • ~=: Contains.

  • ==: Equality operator that tries to coerce one value to the type of the other when the types are different.

7

  • and: Logical and operator.

8

  • or: Logical or operator.

9

  • default: For setting a default value.

  • case and else: For pattern matching.

  • DataWeave functions at this level:

    • matches

    • match

    • map

    • mapObject

    • groupBy

    • filter

  • Binary DataWeave selectors at this level:

    • .@keyName and .*@: Binary DataWeave attribute selectors.

    • .*: Multi-value selector.

    • .^: Metadata selector.

    • .&: Key-value pair selector.

    • [?()]: Filter selector.

  • .keyName: Value selector.

  • [<index> to <index>]: Range selector

10

  • if else: Conditional expression used for flow control in DataWeave.

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.

DataWeave Script:
%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:

Output:
{
  "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:

DataWeave Script:
%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:

Output:
{
  "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|}>}.