I have a flattened JSON that I need to Unflatten.
Flattened version looks like this below - note that “Flower” represents an array hence the “0”.
{
'Trees.ID1.Bud.Growth Season': '',
'Trees.ID1.Bud.Axil': '',
'Trees.ID1.Bud.Flower.0.Sex': '',
'Trees.ID1.Bud.Flower.0.Petal Count ': '',
'Trees.ID1.Bud.Flower.0.Petal Colour': ''
}
I’ve managed to Unflatten, but unfortunately my approach treats the array as a structure.
{
'Trees': {
'ID1': {
'Bud': {
'Growth Season': '',
'Axil': '',
'Flower': {
'0': {
'Sex': '',
'Petal Count ': '',
'Petal Colour': ''
}}}}}}
What I’m trying to work out now is whether I need to adjust my flattened format, or the process I use to UNflatten ?
The Unflatten process I took from this post Python best way to unflat json?
from functools import reduce
def get_nested_default(d, path):
return reduce(lambda d, k: d.setdefault(k, {}), path, d)
def set_nested(d, path, value):
get_nested_default(d, path[:-1])[path[-1]] = value
def unflatten(d, separator='__'):
output = {}
for k, v in d.items():
path = k.split(separator)
set_nested(output, path, v)
return output
Would appreciate any pointers as to best flatten/UNflatten in order to preserve the arrays.