Search code examples
jsonapache-nifijolt

Jolt for deriving a new columns based on substring function ,if-else and then concatenate both the columns


I Need help in deriving the below output using jolt .

Input1:

[
  {
    "season": "Fall",
    "year": "2019"
  }
]

Input2:

[   
  {
    "season": "Spring",
    "year": "2021"
   }
]

condition for season: If it is Spring- Q1,Summer- Q2,Fall- Q3,Holiday- Q4

condition for year : Need to extract last 2 characters

Output1:

{
  "Quarter":"Q3-19"
}

Output2:

{
  "Quarter":"Q1-21"
}

Solution

  • You can apply the following transformations :

    [
      { // to apply the desired conditionals
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&", // to keep the other attribute("year")
            "season": {
              "Spring": { "#Q1": "&2" },
              "Summer": { "#Q2": "&2" },
              "Fall": { "#Q3": "&2" },
              "Holiday": { "#Q4": "&2" }
            }
          }
        }
      },
      { // to handle the string operations among the attributes
        "operation": "modify-overwrite-beta",
        "spec": {
          "year": "=substring(@(1,&),2,4)",
          "Quarter": "=concat(@(1,season),'-',@(1,year))"
        }
      },
      { // return "Quarter" only  
        "operation": "shift",
        "spec": {
          "Qua*": "&"
        }
      }
    ]
    

    the demoes on the site http://jolt-demo.appspot.com/ are :

    enter image description here

    enter image description here