import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
df = pd.DataFrame(data=my_dict)
df.to_json('D:\my_file.json') # Json format file is saved in D drive
This will create a file my_file.json with json formatted data at root of D drive.
df.to_json() # Output as Json string
import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
df = pd.DataFrame(data=my_dict)
df.to_json()
Output
'{"NAME":{"0":"Ravi","1":"Raju","2":"Alex"},"ID":{"0":1,"1":2,"2":3},
"MATH":{"0":30,"1":40,"2":50},"ENGLISH":{"0":20,"1":30,"2":40}}'
import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
df = pd.DataFrame(data=my_dict)
df.to_json(orient='split')
Output
'{"columns":["NAME","ID","MATH","ENGLISH"],"index":[0,1,2],"data":[["Ravi",1,30,20],["Raju",2,40,30],["Alex",3,50,40]]}'
orient='index' ( default )
'{"0":{"NAME":"Ravi","ID":1,"MATH":30,"ENGLISH":20},
"1":{"NAME":"Raju","ID":2,"MATH":40,"ENGLISH":30},
"2":{"NAME":"Alex","ID":3,"MATH":50,"ENGLISH":40}}'
orient='records'
'[{"NAME":"Ravi","ID":1,"MATH":30,"ENGLISH":20},
{"NAME":"Raju","ID":2,"MATH":40,"ENGLISH":30},
{"NAME":"Alex","ID":3,"MATH":50,"ENGLISH":40}]'
orient='columns'
'{"NAME":{"0":"Ravi","1":"Raju","2":"Alex"},
"ID":{"0":1,"1":2,"2":3},"MATH":{"0":30,"1":40,"2":50},
"ENGLISH":{"0":20,"1":30,"2":40}}'
orient='values'
'[["Ravi",1,30,20],["Raju",2,40,30],["Alex",3,50,40]]'
orient='table'
'{"schema": {"fields":[{"name":"index","type":"integer"},{"name":"NAME","type":"string"},{"name":"ID","type":"integer"},{"name":"MATH","type":"integer"},{"name":"ENGLISH","type":"integer"}],"primaryKey":["index"],"pandas_version":"0.20.0"}, "data": [{"index":0,"NAME":"Ravi","ID":1,"MATH":30,"ENGLISH":20},{"index":1,"NAME":"Raju","ID":2,"MATH":40,"ENGLISH":30},{"index":2,"NAME":"Alex","ID":3,"MATH":50,"ENGLISH":40}]}'
import pandas as pd
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student LIMIT 0,10 "
df = pd.read_sql(sql,my_conn)
df.to_json('student1.json',orient='records')
df=pd.read_excel("D:\\my_data\\student.xlsx") # Path of the file.
df.to_json()
We can read one csv file by using read_csv()
df=pd.read_csv("D:\\my_data\\student.csv") # change the path
df.to_json()
to_json()
function in Pandas?to_json()
function to convert a Pandas DataFrame to a JSON string?to_json()
function handle complex data structures, such as nested dictionaries or lists?orient
parameter in the to_json()
function?to_json()
function?to_json()
function?to_json()
function handle missing or NaN values in the DataFrame?to_json()
with the orient
parameter set to "columns" versus "index"?date_format
parameter work in the to_json()
function?to_json()
function?Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.