Search code examples
jsonapache-nifijolt

JOLT - Conditional dynamic default values


I need a transformation that shifts data from one array to another directly. My main problem is that the input dependant array might be empty sometimes. For those cases, there is a master array which defines the length of the result array.

To sum things up, I need to:

  • Shift the data from dependant to result.
  • Specify default values depending on the master array length if no data was shifted.

A couple of examples:

CASE 1 (Shifts data normally)

Input:

{
  "master": [
    {
      "a": "a1",
      "b": "b1"
    },
    {
      "a": "a2",
      "b": "b2"
    }
  ],
  "dependant": [
    {
      "data": "AA"
    },
    {
      "data": "BB"
    }
  ]
}

Desired output:

{
  "result": [ "AA", "BB" ]
}

CASE 2

dependant array is empty and we need to create the result array with the same length as master. Each element can be any string such as DEFAULT.

Input:

{
  "master": [
    {
      "a": "a1",
      "b": "b1"
    },
    {
      "a": "a2",
      "b": "b2"
    }
  ],
  "dependant": []
}

Output:

{
  "result": [ "DEFAULT", "DEFAULT" ]
}

Solution

  • You can consecutively apply modify-overwrite-beta and shift transformations such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "szdependant": "=size(@(1,dependant))"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "szdependant": {
            "0": {
              "@(2,master)": { "*": { "#DEFAULT": "result" } }
            },
            "*": {
              "@(2,dependant)": { "*": { "*": "result" } }
            }
          }
        }
      }
    ]
    

    in order to use the size value(szdependant) of the dependant list as a conditional statement within the shift transformation step