Home > Software design >  Moving a key down into a array in NIFI using JOLT
Moving a key down into a array in NIFI using JOLT

Time:01-12

this is my first post. I need to transform a JSON using JOLT in NIFI. To process it further, I need to move (or copy) the key of each array INTO the array itself.

The JSON is looking like this (simplified, number of elements in the arrays is variable)

{
  "status": {
    "123": {
      "key1": "value1",
      "key2": "value2",
      "key3": "value3",
      "key4": "value4",
      "key5": "value5",
      "key6": "value6",
      "key7": "value7"
    },
    "345": {
      "key1": "value1",
      "key2": "value2",
      "key3": "value3",
      "key4": "value4",
      "key5": "value5",
      "key6": "value6",
      "key7": "value7"
    }
  }
}

I need to have the key of each array being a member of it, so my desired output would be

{
  "status": {
    "123": {
      "id"  : "123"
      "key1": "value1",
      "key2": "value2",
      "key3": "value3",
      "key4": "value4",
      "key5": "value5",
      "key6": "value6",
      "key7": "value7"
    },
    "345": {
      "id"  : "345",
      "key1": "value1",
      "key2": "value2",
      "key3": "value3",
      "key4": "value4",
      "key5": "value5",
      "key6": "value6",
      "key7": "value7"
    }
  }
}

But so far, I wasn't able to solve this. All examples I found were moving a parent value into the array, but never the key of the array itself.

Please help

BR

Kai

CodePudding user response:

You can use a shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.&1.id",
          "*": "&2.&1.&"
        }
      }
    }
  }
]

where $ represents the key at just one level up, &1 stands for the key values of each objects, and &2 is used to grab the literal status by going two levels up.

enter image description here

  •  Tags:  
  • Related