catris25
catris25

Reputation: 1303

How do I convert pandas dataframe to nested JSON object?

I have an SQL database that I need to fetch and convert to JSON. I am thinking that the first step to do that is to fetch the data from the database and load it as a dataframe, then convert the dataframe into JSON object.

Let's say I have the following dataframe.

df_school = pd.DataFrame({'id':[1,2,3,4], 'school_code': ['ABC', 'IJK', 'QRS', 'XYZ'], 'name': ['School A','School B', 'School C', 'School D'], 'type':['private', 'public', 'public', 'private']})
print(df_school)

I want to convert it to JSON with the following code.

import collections

object_list =[]
for idx, row in df_school.iterrows():
    d = collections.OrderedDict()
    d['id'] = row['id']
    d['school_code'] = row['school_code']
    d['name'] = row['name']
    d['type'] = row['type']
    object_list.append(d)

j = json.dumps(object_list)
object_list = 'school_objects.js'
f = open(object_list, 'w')
print(j)

But the result is string. It only looks like a JSON, but when I try to access the item inside the so-called JSON, like j[0] it prints [, not an item inside the JSON.

I also tried another approach, by converting the result from SQL directly to JSON.

query = "Select * from school;"
df_school = pd.read_sql_query(query, connection)
json_school = df_school.head(10).to_json(orient='records')

But I also still got string.

How do I convert it to real JSON object?

Upvotes: 0

Views: 306

Answers (5)

vgp2018
vgp2018

Reputation: 96

data={k:list(v.values()) for k,v in df_school.to_dict().items()}
{
'id': [1, 2, 3, 4],
'school_code': ['ABC', 'IJK', 'QRS', 'XYZ'],
'name': ['School A', 'School B', 'School C', 'School D'],
'type': ['private', 'public', 'public', 'private']
}

Upvotes: 0

Jigy
Jigy

Reputation: 11

import pandas as pd
import json
df_school = pd.DataFrame({'id':[1,2,3,4], 'school_code': ['ABC', 'IJK', 'QRS', 'XYZ'], 'name': ['School A','School B', 'School C', 'School D'], 'type':['private', 'public', 'public', 'private']})
str_school = df_school.to_json(orient='records')
json_school = json.loads(str_school)
json_school[0]

{'id': 1, 'school_code': 'ABC', 'name': 'School A', 'type': 'private'}

Upvotes: 1

Michielver
Michielver

Reputation: 116

Given the provided df_school variable, we can just do j=df_school.to_json(orient='records') to turn it into a JSON formatted string.

Once we have j storing the JSON formatted string, if we want to do something with it, we first have to load the JSON into Python again using json.loads(j). So if we do:

j = df_school.to_json(orient='records')
# parse j into Python
loaded_json = json.loads(j)
print(loaded_json[0])
# print outputs: {'id': 1, 'name': 'School A', 'school_code': 'ABC', 'type': 'private'}

Hope this helps!

Upvotes: 2

Shishir Naresh
Shishir Naresh

Reputation: 763

Try the below code, Hope this will help :

data = [{columns:df_school.iloc[i][columns] for columns in list(df_school.columns)  }  for i in range(df_school.shape[0])   ]

print(data)
print("***********************")
print(type(data[0]))

Ouput will be :

[{'id': 1, 'school_code': 'ABC', 'name': 'School A', 'type': 'private'},
 {'id': 2, 'school_code': 'IJK', 'name': 'School B', 'type': 'public'},
 {'id': 3, 'school_code': 'QRS', 'name': 'School C', 'type': 'public'},
 {'id': 4, 'school_code': 'XYZ', 'name': 'School D', 'type': 'private'}]

*************************
<class 'dict'>

Upvotes: 0

AKX
AKX

Reputation: 168903

JSON is a string encoding of objects.

Once you use json.dumps() or similar, you'll get a string.

Upvotes: 0

Related Questions