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.
You could use flatten_json
:
import json
from flatten_json import flatten, unflatten_list
nested_json = {
'Trees': {
'ID1': {
'Bud': {
'Growth Season': '',
'Axil': '',
'Flower': [{
'Sex': '',
'Petal Count ': '',
'Petal Colour': ''
}]
}
}
}
}
print('Original Nested JSON:')
print(json.dumps(nested_json, indent=4))
flattened = flatten(nested_json, '.')
print('Flattened JSON:')
print(json.dumps(flattened, indent=4))
unflattened = unflatten_list(flattened, '.')
print('Unflattened JSON:')
print(json.dumps(unflattened, indent=4))
Output:
Original Nested JSON:
{
"Trees": {
"ID1": {
"Bud": {
"Growth Season": "",
"Axil": "",
"Flower": [
{
"Sex": "",
"Petal Count ": "",
"Petal Colour": ""
}
]
}
}
}
}
Flattened JSON:
{
"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": ""
}
Unflattened JSON:
{
"Trees": {
"ID1": {
"Bud": {
"Axil": "",
"Flower": [
{
"Petal Colour": "",
"Petal Count ": "",
"Sex": ""
}
],
"Growth Season": ""
}
}
}
}