I have issue with parsing Json file. here the format i have:
{'metadata': {'timezone': {'location': 'Etc/UTC'},
'serial_number': '123456',
'device_type': 'sensor'},
'timestamp': '2019-08-21T13:57:12.500Z',
'framenumber': '4866274',
'tracked_objects': [{'id': 2491,
'type': 'PERSON',
'position': {'x': -361,
'y': -2933,
'type': 'FOOT',
'coordinate_system': 'REAL_WORLD_IN_MILLIMETER'},
'person_data': {'height': 1295}},
{'id': 2492,
'type': 'PERSON',
'position': {'x': -733,
'y': -2860,
'type': 'FOOT',
'coordinate_system': 'REAL_WORLD_IN_MILLIMETER'},
'person_data': {'height': 1928}},
{'id': 2495,
'type': 'PERSON',
'position': {'x': -922,
'y': -3119,
'type': 'FOOT',
'coordinate_system': 'REAL_WORLD_IN_MILLIMETER'},
'person_data': {'height': 1716}}]}
And I am trying to get next columns into dataframe: timezone, serial_number,id, x , y which are part of position, and height.
This is the code i used so far:
# Import Dependencies
import pandas as pd
import json
from pandas.io.json import json_normalize
# loading json file. In your case you will point the data stream into json variable
infile = open("C:/Users/slavi/Documents/GIT/test2.json")
json_raw = json.load(infile)
# Functions to flaten multidimensional json file
def flatten_json(nested_json):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(nested_json)
return out
# Use Function to flaten json
json_flat = flatten_json(json_raw)
# Create panda dataframe from dictionary sine json itself is list of dictionaries or dictiornary of dictionaries
df = pd.DataFrame.from_dict(json_flat, orient='index')
# Reseting index
df.reset_index(level=0, inplace=True)
df.set_index('index', inplace=True)
df
I used the function to flaten the json however when i run the code I am getting results like this:

So there should be 3 lines of data for each tracked object and i should retrieve those columns with 3 lines of data under.
Any suggestion on how to adjust my code?