Search code examples
jsonapache-nifijolt

To Display the attributes, which are taken from the array of objects, flattened in Jolt Transform


I have below jolt input,

{
  "citycode": "34343",
  "branch": "cityname",
  "changeDatetime": 1677063272522,
  "Marks": "L",
  "designations": [
    {
      "designation": "security ",
      "Language": "de"
    },
    {
      "designation": "officer",
      "Language": "fr"
    },
    {
      "designation": "superior",
      "Language": "en"
    }
  ]
}

Desired output is:

{
  "citycode" : "34343",
  "branch" : "cityname",
  "Marks" : "L",
  "LANGUAGE-ID" :  "de", 
  "Occupation" : "security",
  "LANGUAGE-ID" :  "fr",
  "Occupation" : "officer",
  "LANGUAGE-ID" : "en",
  "Occupation" : "superior"
}

current jolt output is below:

{
  "citycode" : "34343",
  "branch" : "cityname",
  "Marks" : "L",
  "LANGUAGE-ID" : [ "de", "fr", "en" ],
  "Occupation" : [ "security ", "officer", "superior" ]
}

Need to split the array elements separately. Can you please help


Solution

  • The current desired JSON output is invalid, but presumably you need the following one

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&", // all elements other than the "designations" array
          "designations": {
            "*": { // loop through the indexes of the "designations" array
              "L*": "LANGUAGE-ID_&1",
              "d*": "Occupation_&1"
            }
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "changeDatetime": ""
        }
      }
    ]
    

    which suffixes incremental(0,1,2) indexes per each LANGUAGE-ID and Occupation key