pd.read_json the table section of a json file

Question:

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

Asked By: SOK

||

Answers:

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
Answered By: Barmar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.