All Collections
Components
Simple IF - ELSE with JOLT
Simple IF - ELSE with JOLT

Applying Conditions to a JSON

Erick Rubiales avatar
Written by Erick Rubiales
Updated over a week ago

IMPORTANT: This documentation has been discontinued. Read the updated IF - ELSE simple with JOLT documentation on our new documentation portal.

Upon performing a TO-FROM to an integration flow, it is very common the need to complete a field on the TO side that does not have its direct correspondent on the FROM side.

However, in some cases, this kind of problem can be solved with a simple "IF-ELSE", from the data coming from the FROM side. 

Example:
I have a JSON with the Client's data:

{
  "client": {
    "name": "Sample client",
    "age": "32",
    "maritalStatus": "Single",
    "country": "Brazil"
  }
}

From the above JSON, I need to create a new JSON containing only the Client's  Name and Citizenship:

{
  "client" : {
    "name" : "Sample client",
    "citizenship" : "Brazilian"
  }
}

For the Citizenship field, only 2 values are accepted:

  • Brazilian

  • Foreigner

However, the first Client's JSON does not have a Citizenship field; it only tells us the client's country of origin.

In this case, we manage to establish the client's citizenship from his country of origin.

Below, there is a simple way of settling this situation, only using a Transformer Connector.

Transformation and "IF-ELSE" with JOLT:

[
  {
    "operation": "shift",
    "spec": {
      "client": {
        "name": "client.name",
        "country": {
          "Brazil": {
            "#Brazilian": "client.citizenship"
          },
          "*": {
            "#Foreigner": "client.citizenship"
          }
        }
      }
    }
  }
]

On the above transformation, we use the same  IF-ELSE principle to check the  "country" value.

Should it be "Brazil", we complete the "citizenship" field with the "Brazilian"
value.

Should the "country" value be any country other than "Brazil", we will complete the "citizenship" field with the "Foreigner" value.

Final JSON with Client's Name and Citizenship:

{
  "client" : {
    "name" : "Sample client",
    "citizenship" : "Brazilian"
  }
}
Did this answer your question?