Support Questions

Find answers, ask questions, and share your expertise

Make flat json array from nested one via JOLT

New Contributor

Hi everyone!

I'm trying to flat array and it view as simple case, but I can't reach the goal.

Here is simple json input:

{
  "MainArray": {
    "mattr1": "some_value1",
    "mattr2": "some_value2",
    "Subarray": {
         "sattr1": 1,
         "sattr2": 2,
         "sattr3": 3
    }
  }
}

Then I would like to convert it to array of arrays [key, value],
Here is Desired Output:

{
  "ResultArray" : [ 
     [ "mattr1", "some_value1" ], 
     [ "mattr2", "some_value2" ], 
     [ "sattr1", 1 ], 
     [ "sattr2", 2 ], 
     [ "sattr1", 3 ] 
  ]
}

 Here is my spec:

[
  {
    "operation": "shift",
    "spec": {
      "MainArray": {
        "*": {
            "$": "ResultArray[#2][0]",
            "@": "ResultArray[#2][1]"
        },
        "Subarray": {
          "*": {
              "$": "ResultArray[#3][#2][0]",
              "@": "ResultArray[#3][#2][1]"
          }
        }
      }
    }
  }
]

 And I don't understand how to fix problem when Subarrays is inside extra array in my result:

{
  "ResultArray" : [ 
     [ "mattr1", "some_value1" ], 
     [ "mattr2", "some_value2" ], 
     [ 
        [ "sattr1", 1 ], 
        [ "sattr2", 2 ], 
        [ "sattr3", 3 ] 
     ] 
  ]
}

 Any advise, please.

1 ACCEPTED SOLUTION

New Contributor

Actually it is not possible to solve this case in one shift - here is discussion and workaround with append subarray to mainarray.

View solution in original post

3 REPLIES 3

Super Collaborator

I'm not sure why your spec is producing this result, but to resolve it you can do the transformation on two shift operations: First to flatting json and move the subarray level to the root level and second to move the result and store it into an Array. Here is the spec I tried and it worked:

 

[
{
"operation": "shift",
"spec": {
"MainArray": {
"*": "ResultArray.&",
"Subarray": {
"*": "ResultArray.&"
}
}
}
},
{
"operation": "shift",
"spec": {
"ResultArray": {
"*": {
"$": "ResultArray[#2][0]",
"@": "ResultArray[#2][1]"
}
}
}
}
]

New Contributor

@SAMSAL Thank for your variant, but it is not actually desired, because Subarray may be another structure of jolt rules inside for example like this

[
  {
    "operation": "shift",
    "spec": {
      "MainArray": {
        "*": {
            "$": "ResultArray[#2][0]",
            "@": "ResultArray[#2][1]"
        },
        "Subarray": {
          "*": {
              "$": "ResultArray[#3][#2][0]",
              "@": "ResultArray[#3][#2][1]",
              "#some_value": "ResultArray[#3][#2][3]"
          }
        }
      }
    }
  }
]

Btw it's my fault cause of not actually correct example (same rules for main and sub arrays)

New Contributor

Actually it is not possible to solve this case in one shift - here is discussion and workaround with append subarray to mainarray.

Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.