Overview
A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator.[1] Common logical operators include AND, OR, and NOT.
Logical operators allow you to write true/false kind of expression.
DataWeave supports the following logical operators:
Expression | Label |
---|---|
A > B | Greater than |
A < B | Less than |
A >= B | Greater than or equal to |
A <= B | Less than or equal to |
A == B | Equal to |
A != B | Not equal to |
A ~= B | Similar to |
not A | Logical negation |
!A | Logical negation |
A and B | Logical and |
A or B | Logical or |
Operator | Description |
---|---|
not | Negates the result of the input. See also, ! . |
! | Negates the result of the input. See also, not . Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later. |
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. |
Note: 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 . |
For example, this script returns true
if the age is less than 10 or false
if it is bigger.
%dw 2.0 output json var age = 2 --- age < 10
true
The following examples use logical operators:Source
%dw 2.0 var myArray = [1,2,3,4,5] var myMap = myArray map not (($ mod 2) == 0) output application/json --- { "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) ] }
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
{ "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 } ] }
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 the orNot
expression.and not
in andNot
.not
and and not
in notWithAndNot
.%dw 2.0 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"} output application/json --- { "answers" : [ orNot, andNot, notWithAndNot ] }
Output
{ "answers": [ { "answer": "orNot - Condition met" }, { "answer": "andNot - Condition met" }, { "answer": "notWithAndNot - Condition met" } ] }
DataWeave executes the code inside each if
block because all conditional expressions in the example evaluate to true
.
If you like my post please follow me to read my latest post on programming and technology.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…