I have json files which look like a dictionary of a list of similar dictionaries:
{"People":[{"FirstName":"Max","Surname":"Smith"},{"FirstName":"Jane","Surname":"Smart"}],
"Animals":[{"Breed":"Cat","Name":"WhiteSocks"},{"Breed":"Dog","Name":"Zeus"}]}
I’m using the following code to convert this into a dictionary of pandas dataframes:
import pandas as pd
import json
# Read the json file
jsonFile = 'exampleJson.json'
with open(jsonFile) as j:
data = json.load(j)
# Convert it to a dictionary of dataframes
dfDict = {}
for dfName, dfContents in data.items():
dfDict[dfName] = pd.DataFrame(dfContents)
display(dfDict[dfName])
The above code gives me exactly what I want, which is a dictionary of dataframes. However it seems rather inefficient. Is there a way to read the json directly into a dictionary of dataframes, rather than reading it into a json object first and then copying that into a dictionary of dataframes? The files I’m working with will be huge.
2
Answers
You should try this code:
You can use json_normalize():