1

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"
    ]
  ]
}

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.