Home > Software engineering >  pd.read_json the table section of a json file
pd.read_json the table section of a json file

Time:08-12

I have the a json file in the following format:

[
{"type":"header","version":"4.9.5deb2","comment":"Export to JSON plugin for PHPMyAdmin"},
{"type":"database","name":"restdb"},
{"type":"table","name":"RestApp_masterlist","database":"restdb","data":
[
    {"id":"1","Year":"2022"},
    {"id":"2","Year":"2022"} 
]
}
]

How can i use pd.read_json to return a dataframe that looks like:

id      Year
1       2022
2       2022

Any help would be greatly appreciated! Thanks

CodePudding user response:

Don't use pd.read_json(). Read the JSON normally, then turn the data: list into the dataframe.

with open("file.json") as f:
    data = json.load(f)

for item in data:
    if item['type'] == 'table':
        df = pd.Dataframe(item['data'])
        break
  • Related