Search code examples
apache-nifijolt

JOLT - Reorder nested arrays


I'm trying to use JOLT to reorder a nested array. My goal is to group all elements that are located in the same array position (i) and add them to another array.

Input:

{
  "values": [
    [
      "84139",
      "123"
    ],
    [
      "230",
      "456"
    ],
    [
      "230475",
      "789"
    ]
  ]
}

Desired result:

{
  "result": [ // same length as values[i]
    [ // same length as values
      "84139",
      "230",
      "230475"
    ],
    [
      "123",
      "456"
      "789"
    ]
  ]
}

INFO: keep in mind the length of both arrays' (root and child) length is variable so the solution has to be generic.

Additional input:

{
  "values": [
    [
      "84139",
      "123",
      "000"
    ],
    [
      "230",
      "456",
      "000"
    ]
  ]
}

Additional output:

{
  "result": [
    [
      "84139",
      "230"
    ],
    [
      "123",
      "456"
    ],
    [
      "000",
      "000"
    ]
  ]
}

Solution

  • Managed to get what you wanted.

    [
      {
        "operation": "shift",
        "spec": {
          "values": {
            "*": {
              "*": "result.[&0]"
            }
          }
        }
      }
    ]
    

    The secret here is matching the "leaf nodes" of values to the index they occupy in their respective arrays by using &0, which in this case indicates the array index of each leaf node.

    Be aware that this solution will only work if all the children of values have the same number of children.