Python – How To Convert Pandas Dataframe To JSON Object?

Question:

I’m using df.to_json() to convert dataframe to json. But it gives me a json string and not an object.

How can I get JSON object?

Also, when I’m appending this data to an array, it adds single quote before and after the json and it ruins the json structure.

How can I export to json object and append properly?

Code Used:

a=[]
     array.append(df1.to_json(orient='records', lines=True)) 
     array.append(df2.to_json(orient='records', lines=True)) 

Result:

['{"test:"w","param":1}','{"test:"w2","param":2}]']

Required Result:

[{"test":"w","param":1},{"test":"w2","param":2}]

Thank you!

Asked By: jason

||

Answers:

I believe need create dict and then convert to json:

import json
d = df1.to_dict(orient='records')
j = json.dumps(d)

Or if possible:

j = df1.to_json(orient='records')
Answered By: jezrael

Here’s what worked for me:

import pandas as pd
import json

df = pd.DataFrame([{"test":"w","param":1},{"test":"w2","param":2}])
print(df)
    test  param
0     w      1
1    w2      2

So now we convert to a json string:

d = df.to_json(orient='records')
print(d)
'[{"test":"w","param":1},{"test":"w2","param":2}]'

And now we parse this string to a list of dicts:

data = json.loads(d)
print(data)
[{'test': 'w', 'param': 1}, {'test': 'w2', 'param': 2}]
Answered By: igorkf
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.