Logical Operators

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:

Operators

ExpressionLabel
A > BGreater than
A < BLess than
A >= BGreater than or equal to
A <= BLess than or equal to
A == BEqual to
A != BNot equal to
A ~= BSimilar to
not ALogical negation
!ALogical negation
A and BLogical and
A or BLogical or

OperatorDescription
notNegates 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.
andReturns true if the result of all inputs is true, false if not.
orReturns 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 Script:

%dw 2.0
output json
var age = 2
---
age < 10

Output:

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.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago