read_json()

Read and display data from json file.
import pandas as pd 
df=pd.read_json("student.json")
print(df)
Output is here
    class  id  mark         name     sex
0    Four   1    75     John Deo  female
1   Three   2    85     Max Ruin    male
2   Three   3    55       Arnold    male
3    Four   4    60   Krish Star  female
4    Four   5    60    John Mike  female
------------
------------
read_json(): Creating Datafram from Json string #B08



This will read the data from json file ( in same directory ) student.json
We can read the URL also.
import pandas as pd 
df=pd.read_json("https://www.plus2net.com/php_tutorial/student.json")
print(df)
It can be your localhost also.
import pandas as pd 
df=pd.read_json("http://127.0.0.1/student.json")
print(df)

Options

We can generate JSON ( to_json() )by using various orient options and while reading we can maintain the same orient option.

orient='split'

df=pd.DataFrame(data=my_dict)
df_j=df.to_json(orient='split')
print(df_j)
Output is here
{"columns":["NAME","ID","MATH","ENGLISH"],"index":[0,1,2],"data":[["Alex",1,50,50],["Ravi",2,36,48],["Ron",3,45,49]]}
To read the above JSON format, we have to maintain the same orient value as option.
df=pd.read_json(df_j,orient='split')
print(df)

orient='records'

[{"NAME":"Alex","ID":1,"MATH":50,"ENGLISH":50},{"NAME":"Ravi","ID":2,"MATH":36,"ENGLISH":48},{"NAME":"Ron","ID":3,"MATH":45,"ENGLISH":49}]
df=pd.read_json(df_j,orient='records')

orient=index

{"0":{"NAME":"Alex","ID":1,"MATH":50,"ENGLISH":50},"1":{"NAME":"Ravi","ID":2,"MATH":36,"ENGLISH":48},"2":{"NAME":"Ron","ID":3,"MATH":45,"ENGLISH":49}}
df=pd.read_json(df_j,orient='index')

orient='columns'

{"NAME":{"0":"Alex","1":"Ravi","2":"Ron"},"ID":{"0":1,"1":2,"2":3},"MATH":{"0":50,"1":36,"2":45},"ENGLISH":{"0":50,"1":48,"2":49}}
df=pd.read_json(df_j,orient='index')

orient='values'

[["Alex",1,50,50],["Ravi",2,36,48],["Ron",3,45,49]]
df=pd.read_json(df_j,orient='values')

lines

default value is False,
Read the file as a json object per line. This will work
df=pd.read_json("https://www.plus2net.com/php_tutorial/student2.json",lines=True)
Here if we use lines=False then we will get error message.
For comparing, same data is used in two different formats.
https://www.plus2net.com/php_tutorial/student2.json
https://www.plus2net.com/php_tutorial/student.json

Python Pandas reading JSON format data from URL files and dataframes using read_json() with options


Questions



Pandas read_csv() to_csv() to_excel()
Data input and output from Pandas DataFrame

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer