Search code examples
jsonapache-nifijolt

JOLT transform JSON merge array, but values from another fields


I need to extract values from primary_keys array and join them into one string. But I'm having troubles with values extraction.

Simplified input json:

{
  "primary_keys": [
    "ITEM",
    "LOC",
    "COMP_ID"
  ],
  "ITEM": "ID158",
  "LOC": 41,
  "COMP_ID": "BPF",
  "VALUE": 0.78
}

Expected output:

{
  "PK": "ID158|41|BPF",
  "ITEM": "ID158",
  "LOC": 41,
  "COMP_ID": "BPF",
  "VALUE": 0.78
}

Content of primary_keys array may differ from one flowfile to another. I appreciate any input. thanks!


Solution

  • You can use modify-overwrite-beta transformation along with join function after deriving an array(PK) composed of the respective values for the each members of primary_keys array through use of shift transformations such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "primary_keys": {
            "*": {
              "*": { "$": "PK.@(4,&)" }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "PK": {
            "*": {
              "$": "&2"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "PK": "=join('|',@(1,&))"
        }
      }
    ]
    

    enter image description here