%dw 2.0
output application/json

{ "math" : [
{ "2 + 2" : (2 + 2) },
{ "2  2" : (2  2) },
{ "2 * 2" : (2 * 2) },
{ "2 / 2" : (2 / 2) }
]
}
DataWeave Operators
DataWeave 2.0 supports several mathematical, equality, relational, logical, prepend, and append operators.
Mathematical Operators
DataWeave 2.0 supports the most common mathematical operators:
Operator  Description 


For addition 

For subtraction 

For multiplication. 

For division. 
These examples use mathematical operators:
"math": [
{ "2 + 2": 4 },
{ "2  2": 0 },
{ "2 * 2": 4 },
{ "2 / 2": 1.0 }
]
}
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
.
These examples use 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) }
]
}
"relational": [
{ "(1 < 1)": false },
{ "(1 > 2)": false },
{ "(1 <= 1)": true },
{ "(1 >= 1)": true }
]
}
These examples use equality operators:
%dw 2.0
output application/dw

{ "equality" :
[
(1 == 1),
(1 == 2),
("true" == true),
("true" ~= true),
(['true'] ~= [true]),
('1' ~= 1)
]
}
{
equality: [ true, false, false, true, true, true ]
}
Logical Operators
The following logical operators are supported.
Operator  Description 


Negates the result of the input. 

Returns 

Returns 
These examples show uses of these 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)
]
}
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 }
]
}
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 next example uses or not
as defined in the orNot
expression, uses and not
in andNot
, and uses not
and and not
in notWithAndNot
.
%dw 2.0
output application/json
var orNot = if (1 + 1 == 4 or not 1 == 2) {"answer": "foo"}
else {"answer": "nope"}
var andNot = if (1 + 1 == 2 and not 1 == 2) {"answer": "bar"}
else {"answer": "nope"}
var notWithAndNot = if (not (1 + 1 == 2 and not 1 == 1)) {"answer": "foobar"}
else {"answer": "nope"}

{ "answers" :
[
orNot,
andNot,
notWithAndNot
]
}
{
"answers": [
{ "answer": "foo" },
{ "answer": "bar" },
{ "answer": "foobar" }
]
}
Prepend and Append Operators for Arrays
DataWeave 2.0 supports operators for appending and prepending items within an array.
Operator  Description 


Prepends data on the lefthand side of the operator to items in the
array on the righthand side. For example, 

Appends data on the righthand side of the operator to items in the
array on the lefthand side. For example, 

Appends data on the righthand side of the operator to items in the
array on the lefthand side. For example, 
These examples show uses of these operators:
%dw 2.0
output application/json

{
"prependappend" : [
// Array on right side when prepending.
{ "prepend" : 1 >> [2] },
{ "prependnumber" : 1 >> [1] },
{ "prependstring" : "a" >> [1] },
{ "prependobject" : { "a" : "b"} >> [1] },
{ "prependarray" : [1] >> [2, 3] },
{ "prependbinary" : (1 as Binary) >> [1] },
{ "prependdatetime" : 23:57:59Z >> [ 20171001 ] },
// Array is on left side when appending.
{ "appendnumber" : [1] << 2 },
{ "appendstring" : [1] << "a" },
{ "appendobject" : [1] << { "a" : "b"} },
{ "appendarray" : [1,2] << [1, 2, 3] },
{ "appendbinary" : [1] << (1 as Binary) },
{ "appenddatetime" : [ 20171001 ] << 23:57:59Z },
{ "appendobjecttoarray" : [1,2] << {"a" : "b"} },
{ "appendarraytoarray1" : ["a","b"] << ["c","d"] },
{ "appendarraytoarray2" : [["a","b"],["c","d"]] << ["e","f"] },
// + always appends within the array
{ "appendwith+" : [1] + 2 },
{ "appendwith+" : [2] + 1 }
]
}
{
"prependappend": [
{ "prepend": [ 1, 2 ] },
{ "prependnumber": [ 1, 1 ] },
{ "prependstring": [ "a", 1 ] },
{ "prependarray": [ [ 1 ], 2, 3 ] },
{ "prependobject": [ { "a": "b" }, 1 ] },
{ "prependbinary": [ "\u0001", 1 ] },
{ "prependdatetime": [ "23:57:59Z", "20171001" ] },
{ "appendnumber": [ 1, 2 ] },
{ "appendstring": [ 1, "a" ] },
{ "appendobject": [ 1, { "a": "b" } ] },
{ "appendarray": [ 1, 2, [ 1, 2, 3 ] ] },
{ "appendbinary": [ 1, "\u0001" ] },
{ "appenddatetime": [ "20171001", "23:57:59Z" ] },
{ "appendobjecttoarray": [ 1, 2, { "a": "b" } ] },
{ "appendarraytoarray1": [ "a", "b", ["c","d"] ] },
{ "appendarraytoarray2": [ ["a","b"], ["c","d"], ["e","f"] ] },
{ "appendwith+": [ 1, 2] },
{ "appendwith+": [ 2, 1] }
]
}