Nav

DataWeave Operators

DataWeave 2.0 supports several mathematical, equality, relational, and logical operators.

Mathematical Operators

DataWeave supports the most common mathematical operators:

Operator Description

+

For addition

-

For subtraction

*

For multiplication.

/

For division.

These examples use mathematical operators:

Source

         
      
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
---
{ "math" : [
    { "2 + 2" : (2 + 2) },
    { "2 - 2" : (2 - 2) },
    { "2 * 2" : (2 * 2) },
    { "2 / 2" : (2 / 2) }
  ]
}
Output

         
      
1
2
3
4
5
6
7
"math": [
    { "2 + 2": 4 },
    { "2 - 2": 0 },
    { "2 * 2": 4 },
    { "2 / 2": 1.0 }
  ]
}

Note that several DataWeave functions operate on numbers, for example: sum, mod (for modulo), and avg (for average).

Equality and Relational Operators

DataWeave 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.

These examples use relational operators:

Source

         
      
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
---
{ "relational" : [
    { "1 < 1" : (1 < 1) },
    { "1 > 2" : (1 > 2) },
    { "1 <= 1" : (1 <= 1) },
    { "1 >= 1" : (1 >= 1) }
  ]
}
Output

         
      
1
2
3
4
5
6
7
"relational": [
    { "(1 < 1)": false },
    { "(1 > 2)": false },
    { "(1 <= 1)": true },
    { "(1 >= 1)": true }
  ]
}

These examples use equality operators:

Source

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
output application/dw
---
{ "equality" :
  [
    (1 == 1),
    (1 == 2),
    ("true" == true),
    ("true" ~= true),
    (['true'] ~= [true]),
    ('1' ~= 1)
  ]
}
Output
{
  equality: [ true, false, false, true, true, true ]
}

Logical Operators

The following logical operators are supported.

Operator Description

not

Negates the result of the input.

and

Returns true if the result of all inputs is true, false if not.

or

Returns true if the result of any input is true, false if not.

These examples show uses of these operators:

Source

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
%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.

Output

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
  "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.

Example: Using Logical Operators Together

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%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
  ]
}
Output

         
      
1
2
3
4
5
6
7
{
  "answers": [
    { "answer": "foo" },
    { "answer": "bar" },
    { "answer": "foobar" }
  ]
}

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.

+