convert pandas dataframe to json object - pandas

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}]

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')