Defining a value based on multiple conditions - 7.3

Talend Data Mapper Functions Reference Guide

Version
7.3
Language
English
Product
Talend Big Data Platform
Talend Data Fabric
Talend Data Management Platform
Talend Data Services Platform
Talend MDM Platform
Talend Real-Time Big Data Platform
Module
Talend Studio
Content
Design and Development > Designing Jobs
Last publication date
2023-01-05

Use nested IfThenElse functions with And, Equal, Greater and Lesser functions to define the value of an element based on several conditions.

Before you begin

You have created a map with an input and an output structure. You can use the samples provided below.

About this task

In this example, you have a JSON file containing a list of items with their ID and the number of items in stock. You want to create a CSV output with the item ID and a status related to the number of items in stock: out of stock if the number is 0, low stock if the number is between 1 and 99, and in stock if it is 100 or above. You can use the following JSON as input:
{
    "items": [
        {
            "id" : 654981,
            "stock": 43
        },
        {
            "id" : 698624,
            "stock": 295
        },
        {
            "id" : 149875,
            "stock": 0
        },
        {
            "id" : 698763,
            "stock": 100
        },
        {
            "id" : 856231,
            "stock": 15
        },
        {
            "id" : 789354,
            "stock": 457
        }
    ]
}
You can use the following CSV sample to create your output structure:
itemId,stockStatus

Procedure

  1. Drag and drop the input id element on the output itemId element.
  2. Click the stockStatus element and drag and drop an IfThenElse function in the Value tab.
  3. Drag and drop an Equal function on the Condition argument, then drag and drop the input stock element on the First Value argument.
  4. Drag and drop a Constant function on the Second Value argument, then double-click it, enter 0 in the Value field, select Integer (32) in the Data Type field and click OK.
  5. Add a Constant function on the Then argument and enter out of stock in its Value field.
  6. Add another IfThenElse function on the Else argument.
    This allows you to define another condition with a different value.
  7. Add an And function on the Condition argument, then add a Greater and a Lesser function on the And function.
  8. Drag and drop the input stock element on the First Value argument of the Greater and Lesser functions.
  9. Add a Constant function on the Second Value argument of the Greater and Lesser functions.
    1. For the Constant function under Greater, enter 0 in the Value field.
    2. For the Constant function under Lesser, enter 100 in the Value field.
  10. Add a Constant function on the Then argument and enter low stock in its Value field.
  11. Add a Constant function on the Else argument and enter in stock in its Value field.

Results

Your map is configured and should look like this:
You can use the Test Run feature to see the result. In this example, the following output is returned:
itemId,stockStatus
654981,low stock
698624,in stock
149875,out of stock
698763,in stock
856231,low stock
789354,in stock