Here is the example JSON:
{
"ApartmentBuilding":{
"Address":{
"HouseNumber": 5,
"Street": "DataStreet",
"ZipCode": 5100
},
"Apartments":[
{
"Number": 1,
"Price": 500,
"Residents": [
{
"Name": "Bob",
"Age": 43
},
{
"Name": "Alice",
"Age": 42
}
]
},
{
"Number": 2,
"Price": 750,
"Residents": [
{
"Name": "Jane",
"Age": 43
},
{
"Name": "William",
"Age": 42
}
]
},
{
"Number": 3,
"Price": 1000,
"Residents": []
}
]
}
}
I used below function from : Python Pandas - Flatten Nested JSON
import json
import pandas as pd
def flatten_json(nested_json: dict, exclude: list=['']) -> dict:
"""
Flatten a list of nested dicts.
"""
out = dict()
def flatten(x: (list, dict, str), name: str='', exclude=exclude):
if type(x) is dict:
for a in x:
if a not in exclude:
flatten(x[a], f'{name}{a}_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, f'{name}{i}_')
i += 1
else:
out[name[:-1]] = x
flatten(nested_json)
return out
with open("apartments.json") as f:
data = json.load(f)
print(data)
df = pd.DataFrame([flatten_json(x) for x in data['ApartmentBuilding']])
print(df)
I am aiming to completely flatten the JSON to be transformed into a Panda Dataframe however, get a strange output as belows:
0 Address
1 Apartments
I am basically after something flatten like this: