%dw 2.0
output application/json
---
{ "mathOperators" : [
{ "2 + 2" : (2 + 2) },
{ "2 - 2" : (2 - 2) },
{ "2 * 2" : (2 * 2) },
{ "2 / 2" : (2 / 2) },
{ "[1,2,3] - 1 + 4" : [1,2,3] - 1 + 4},
{ "{a:1, b:2, c:3} - 'a' " : {a:1, b:2, c:3} - "a"},
{ "|2021-03-02T10:39:59| - |P1D| + |PT3H|" : |2021-03-02T10:39:59| - |P1D| + |PT3H|}
]
}
dataweave
DataWeave Operators
DataWeave 2.2 is compatible and bundled with Mule 4.2.
This version of Mule reached its
End of Life 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 2.0 supports several mathematical, equality, relational, logical, prepend, append, flow control, and scope operators. Before you begin, note that DataWeave version 2 is for Mule 4 apps. For Mule 3 apps, refer to DataWeave Operators in the Mule 3.9 documentation. For other Mule versions, you can use the version selector for the Mule Runtime table of contents.
Mathematical Operators
DataWeave 2.0 supports the most common mathematical operators:
Operator | Description |
---|---|
|
For addition. |
|
For subtraction. |
|
For multiplication. |
|
For division. |
In addition to operating with numbers, the (-)
and (+)
operators can also operate with complex data structures like arrays, objects, and dates.
The following example uses mathematical operators with different data types:
{
"mathOperators": [
{ "2 + 2": 4 },
{ "2 - 2": 0 },
{ "2 * 2": 4 },
{ "2 / 2": 1 },
{ "[1,2,3] - 1 + 4": [2,3,4] },
{ "{a:1, b:2, c:3} - 'a' ": {"b": 2, "c": 3} },
{ "|2021-03-02T10:39:59| - |P1D| + |PT3H|": "2021-03-01T13:39:59" }]
}
json
Equality and Relational Operators
DataWeave 2.0 supports the following equality and relational operators:
Operator | Description |
---|---|
|
For less than. |
|
For greater than. |
|
For less than or equal to. |
|
For greater than or equal to. |
|
For equal to. |
|
Equality operator that tries to coerce one value to the type of the other when the types are different. |
Note that you can negate these operators by using the logical operator, not
.
The following example uses relational operators:
%dw 2.0
output application/json
---
{ "relational" : [
{ "1 < 1" : (1 < 1) },
{ "1 > 2" : (1 > 2) },
{ "1 <= 1" : (1 <= 1) },
{ "1 >= 1" : (1 >= 1) }
]
}
dataweave
{ "relational": [
{ "(1 < 1)": false },
{ "(1 > 2)": false },
{ "(1 <= 1)": true },
{ "(1 >= 1)": true }
]
}
json
Note that if the operands of the relational operator belong to different types,
DataWeave coerces the right-side operand to the type of the left-side operand.
For example, in the expression "123" > 12
DataWeave coerces 12
(a Number type)
to "12"
(a String type) and compares each String value lexicographically.
In the expression 123 > "12"
, DataWeave coerces the String value "12"
to the Number value 12
and compares the numbers.
These examples use equality operators:
%dw 2.0
output application/dw
---
{ "equality" :
[
(1 == 1),
(1 == 2),
("true" == true),
("true" ~= true),
(['true'] ~= [true]),
('1' ~= 1)
]
}
dataweave
{ equality: [ true, false, false, true, true, true ] }
text
Logical Operators
DataWeave 2.0 supports the following logical operators:
Operator | Description |
---|---|
|
Negates the result of the input. See also, |
|
Negates the result of the input. See also, |
|
Returns |
|
Returns |
Though the semantics of not and ! are the same, their precedence
differs. not true or true is executed as not (true or true) ,
so it returns false , whereas !true or true returns true because
the ! only applies to the first true . !(true or true) returns
false .
|
The following examples use logical operators:
%dw 2.0
output application/json
var myArray = [1,2,3,4,5]
var myMap = myArray map not (($ mod 2) == 0)
---
{
"not" : [
"notTrue" : not true,
"notFalse" : not false,
"myMapWithNot" : myMap
],
"and" : [
"andTrueFalse" : true and false,
"andIsTrue" : (1 + 1 == 2) and (2 + 2 == 4),
"andIsFalse" : (1 + 1 == 2) and (2 + 2 == 2)
],
"or" : [
"orTrueFalse" : true or false,
"orIsTrue" : (1 + 1 == 2) or (2 + 2 == 2),
"orIsFalse" : (1 + 1 == 1) or (2 + 2 == 2)
],
"!-vs-not" : [
"example-!" : (! true or true),
"example-not" : (not true or true)
]
}
dataweave
Note that myMap
iterates through the items in a list (myArray
) and
determines whether the modulo (mod
) expression does not evaluate to 0
when
applied to each given item.
{
"not": [
{ "notTrue": false },
{ "notFalse": true },
{ "myMapWithNot": [ true, false, true, false, true ] }
],
"and": [
{ "andTrueFalse": false },
{ "andIsTrue": true },
{ "andIsFalse": false }
],
"or": [
{ "orTrueFalse": true },
{ "orIsTrue": true },
{ "orIsFalse": false }
],
"!-vs-not": [
{ "example-!": true },
{ "example-not": false }
]
}
json
Note that not
works in expressions such as not (true)
, but not(true)
(without the space) does not work.
You can use logical operators together. The following example uses:
-
or not
as defined in theorNot
expression. -
and not
inandNot
. -
not
andand not
innotWithAndNot
.
%dw 2.0
output application/json
var orNot = if (1 + 1 == 4 or not 1 == 2) {"answer": "orNot - Condition met"}
else {"answer": "nope"}
var andNot = if (1 + 1 == 2 and not 1 == 2) {"answer": "andNot - Condition met"}
else {"answer": "nope"}
var notWithAndNot = if (not (1 + 1 == 2 and not 1 == 1)) {"answer": "notWithAndNot - Condition met"}
else {"answer": "nope"}
---
{ "answers" :
[
orNot,
andNot,
notWithAndNot
]
}
dataweave
{
"answers": [
{ "answer": "orNot - Condition met" },
{ "answer": "andNot - Condition met" },
{ "answer": "notWithAndNot - Condition met" }
]
}
json
DataWeave executes the code inside each if
block because all conditional expressions in the example evaluate to true
.
Prepend, Append, and Remove Operators for Arrays
DataWeave 2.0 supports operators for appending and prepending items within an array:
Operator | Description |
---|---|
|
Prepends data on the left-hand side of the operator to items in the
array on the right-hand side. For example, |
|
Appends data on the right-hand side of the operator to items in the
array on the left-hand side. For example, |
|
Appends data on the right-hand side of the operator to items in the
array on the left-hand side. For example, |
|
Removes a specified element of any supported type from an array. |
The following examples show uses of prepend, append, and remove operators on arrays:
%dw 2.0
output application/json
---
{
"prepend-append" : [
// Array on right side when prepending.
{ "prepend" : 1 >> [2] },
{ "prepend-number" : 1 >> [1] },
{ "prepend-string" : "a" >> [1] },
{ "prepend-object" : { "a" : "b"} >> [1] },
{ "prepend-array" : [1] >> [2, 3] },
{ "prepend-binary" : (1 as Binary) >> [1] },
{ "prepend-date-time" : |23:57:59Z| >> [ |2017-10-01| ] },
// Array is on left side when appending.
{ "append-number" : [1] << 2 },
{ "append-string" : [1] << "a" },
{ "append-object" : [1] << { "a" : "b"} },
{ "append-array" : [1,2] << [1, 2, 3] },
{ "append-binary" : [1] << (1 as Binary) },
{ "append-date-time" : [ |2017-10-01| ] << |23:57:59Z| },
{ "append-object-to-array" : [1,2] << {"a" : "b"} },
{ "append-array-to-array1" : ["a","b"] << ["c","d"] },
{ "append-array-to-array2" : [["a","b"],["c","d"]] << ["e","f"] },
// + always appends within the array
{ "append-with-+" : [1] + 2 },
{ "append-with-+" : [2] + 1 },
{ "removeNumberFromArray" : ( [1,2,3] - 2 ) },
{ "removeObjectFromArray" : ( [ {a : "b"}, {c : "d"} , { e : "f"} ] - { c : "d"} ) }
]
}
dataweave
{
"prepend-append": [
{ "prepend": [ 1, 2 ] },
{ "prepend-number": [ 1, 1 ] },
{ "prepend-string": [ "a", 1 ] },
{ "prepend-array": [ [ 1 ], 2, 3 ] },
{ "prepend-object": [ { "a": "b" }, 1 ] },
{ "prepend-binary": [ "\u0001", 1 ] },
{ "prepend-date-time": [ "23:57:59Z", "2017-10-01" ] },
{ "append-number": [ 1, 2 ] },
{ "append-string": [ 1, "a" ] },
{ "append-object": [ 1, { "a": "b" } ] },
{ "append-array": [ 1, 2, [ 1, 2, 3 ] ] },
{ "append-binary": [ 1, "\u0001" ] },
{ "append-date-time": [ "2017-10-01", "23:57:59Z" ] },
{ "append-object-to-array": [ 1, 2, { "a": "b" } ] },
{ "append-array-to-array1": [ "a", "b", ["c","d"] ] },
{ "append-array-to-array2": [ ["a","b"], ["c","d"], ["e","f"] ] },
{ "append-with-+": [ 1, 2] },
{ "append-with-+": [ 2, 1] },
{ "removeNumberFromArray": [ 1, 3 ] },
{ "removeObjectFromArray": [ { "a": "b" }, { "e": "f" } ] }
]
}
json
Scope and Flow Control Operators
DataWeave 2.0 supports operators that control the flow and scope of expressions:
Operator | Description |
---|---|
|
Creates a scope in which new variables, functions, annotations, or namespaces can be declared and used. The syntax is similar to a mapping in that it is composed of a header and body separated by |
|
Replaced by |
Operator | Description |
---|---|
|
An |
|
An |