Read json file as pandas dataframe?
From your code, it looks like you're loading a JSON file which has JSON data on each separate line. read_json
supports a lines
argument for data like this:
data_df = pd.read_json('C:/Users/Alberto/nutrients.json', lines=True)
Note
Removelines=True
if you have a single JSON object instead of individual JSON objects on each line.
Using the json module you can parse the json into a python object, then create a dataframe from that:
import jsonimport pandas as pdwith open('C:/Users/Alberto/nutrients.json', 'r') as f: data = json.load(f)df = pd.DataFrame(data)
If you open the file as binary ('rb'
), you will get bytes. How about:
with open('C:/Users/Alberto/nutrients.json', 'rU') as f:
Also as noted in this answer you can also use pandas directly like:
df = pd.read_json('C:/Users/Alberto/nutrients.json', lines=True)