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)
Answer from coldspeed95 on Stack OverflowNote
Removelines=Trueif you have a single JSON object instead of individual JSON objects on each line.
Videos
How to load JSON file with Pandas?
import pandas as pd df = pd.read_json('file.json')
This reads the JSON file into a Pandas DataFrame.
How to read a JSON column in Pandas?
import pandas as pd df = pd.DataFrame({'col': ['{"name": "John", "age": 30}', '{"name": "Jane", "age": 25}']}) df['col'] = df['col'].apply(pd.json_normalize)
This will convert the JSON strings in the column into structured data.
How to load JSON string into Pandas DataFrame?
import pandas as pd import json json_string = '{"name": "John", "age": 30}' df = pd.read_json(json.loads(json_string))
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=Trueif 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 json
import pandas as pd
with open('C:/Users/Alberto/nutrients.json', 'r') as f:
data = json.load(f)
df = pd.DataFrame(data)
Doing it with pure Python is interesting. It's incredibly flexible. It's time consuming. Is it silly?
I'm generally up for doing things the native way just because it's clean. But am I being silly not abstracting it away with some package? I was using a flavor of SQL I rarely touch the other day and was told "now with JSON support" and it actually wasn't terrible. SQL isn't exactly a bastion of exclusively new thinking. If we've already eliminated actual javascript for dealing with its JSON, why stop there? I am becoming a back in the good ole days when we used horses type of ass?
first check if it's a valid json file or not using JSON validator site
once the file is in valid json format you can use the below code to read it as dataframe
with open("training.json") as datafile:
data = json.load(datafile)
dataframe = pd.DataFrame(data)
hope this helps.
read_json() can't work because of the new line after "pqr". You can either try and fix that line or try and format the whole thing into valid JSON. I'm doing the latter here by adding commas after new lines and surrounding the whole thing with brackets to form a proper JSON array:
with open('temp.txt') as f:
content = f.read()
pd.read_json('[' + content.replace('}\n', '},') + ']')