if else Condition in Flow Control (DataWeave 2.0)

if else

An if statement evaluates a conditional expression and returns the value under the if only if the conditional expression returns true. Otherwise, it returns the expression under else. Every if expression must have a matching else expression.

If/else expressions allow you to make decisions using logical operators and branch as a result.

operators and branch as a result.

if (<criteria_expression&gt;) <return_if_true&gt; else <return_if_false&gt;

There are cases in DW where parentheses are optional, but it’s important to note the criteria must be surrounded by parentheses in if/else expressions.

The following example uses the input { country : “India” }, which is defined by the myVar variable in the header:

DataWeave Script:

%dw 2.0
var myVar = { country : "India" }
output application/json
---
if (myVar.country == "UAE")
  { currency: "EUR" }
else { currency: "RUPEES" }

Output:

{
  "currency": "RUPEES"
}

You can use the if-else construct on any condition that evaluates to true or false, including mathematical, logical, equality, and relational statements. The condition can act on any valid input.

  • A mathematical operation in ex1
    The if else statement returns the Boolean value true if the operation 1 + 1 == 55 is true and false if not.
  • An equality operation in ex2
    The if else statement returns 1 if the value of the specified index is 1 or a string if the value is not 1.
  • An isEmpty function in ex3
    The if else statement returns the string “ID is empty” or “ID is not empty” depending on whether aRecord.bookId contains a value.

A mapping in ex4 that iterates over firstInput
The if else statement returns the value of the bookId as a Number if the value is equal to 101. It returns the specified string if the value is not equal to 101.

DataWeave Script:

%dw 2.0
var aRecord =
 [
    "bookId":"101",
    "title":"world history",
    "price":"19.99"
 ]
output application/xml
---
{ examples:
 {
    ex1 : if (1 + 1 == 55) true
          else false,
    ex2 : if ([1,2,3,4][1] == 1) 1
          else "value of index 1 is not 1",
    ex3 : if (isEmpty(aRecord.bookId)) "ID is empty"
     else "ID is not empty",
    ex4 : aRecord.bookId map (idValue) -&gt;
      if (idValue as Number == 101) idValue as Number
   else "not 101"
 }
}

Output:

<?xml version='1.0' encoding='UTF-8'?>
<examples>
  <ex1>false</ex1>
  <ex2>value of index 1 is not 1</ex2>
  <ex3>ID is not empty</ex3>
  <ex4>101</ex4>
</examples>






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

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

4 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

4 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

4 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

5 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

5 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

5 months ago