Skip to content

Flatten and Unflatten JSON #1729

Answered by mihaitodor
taythebot asked this question in Q&A
Feb 7, 2023 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

Hey @taythebot! For flattening, try using the collapse() method. For example:

{"foo" :{"bar": {"baz": 666}}, "a": ["b", "c"]}

will be collapsed to:

{
  "a.0": "b",
  "a.1": "c",
  "foo.bar.baz": 666
}

To uncollapse the result back to the original input, there's currently no utility for it and it's a bit tricky to implement it since it would need to support arbitrary levels of intermixed objects arrays. And speaking of arrays, this utility would also need to check if there are gaps in the indexes, like having only "foo.0" and "foo.2" without "foo.1", and raise an error if it detects this. I tried to implement it using bloblang and I was able to expand each key into the correct structure, b…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@taythebot
Comment options

@mihaitodor
Comment options

@taythebot
Comment options

@mihaitodor
Comment options

Answer selected by taythebot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants