I am using python to flatten and unpack JSON structures. I have already figured out flattening and can flatten JSON files into dictionary structures like this:
# Given the JSON
{
"a": "thing",
"b": {
"c": "foo"
},
"array": [
"item1",
"item2"
]
}
Then flatten() it into:
{
"a": "thing",
"b.c": "foo",
"array.[0]": "item1",
"array.[1]": "item2"
}
But any ideas on how to unpack those flattened dicts back into the original json? I had an idea on how to do it using string.split() on the key names but the arrays complicated things and now I don't know how to go about doing it. The trouble is arrays can have items that themselves are another array or dict. I am guessing something recursive?
UPDATE: So I have looking around for packages that unflatten (or flatten + unflatten, I don't care) and I found this one, which works well except it can't handle paths that include the separator character as part of the key name.
For example I had a path that flattened down into REG_SRC.http://www.awebsite.com/
but when unflattened, it got a little mangled because the dots in the URL were interpreted as key seperators. Does anyone know of a library that can handle key names with any text? Even text containing the separator character? I am assuming it would require the flat paths to be quote encapsulated or something "REG_SRC"."http://www.awebsite.com/"