i have dataframe User
id | user | phone
------------------
1 | chris | {"device": "a", "brand": "a"}
2 | nat | {"device": "a", "brand": "a"}
3 | zack | {"device": "a", "brand": "a"}
then pass it to object variable
rows = df.loc[df['id'] == 1]
merged = {'data': rows.reindex(columns=df.columns.values).to_dict('records')}
with open((path + '/' + filename + '.json'), 'w') as fp:
json.dump(merged, fp)
then got filename.json:
{
"data": {
"id" : 1,
"user" : "chris",
"phone": "{\"device\": \"a\", \"brand\": \"a\"}"
}
}
expected result:
{
"data": {
"id" : 1,
"user" : "chris",
"phone": {
"device": "a",
"brand": "a"
}
}
}
how to make it as expected result?
The content of phone
is already JSON. Converting JSON to JSON will lead to escaping the quotes, as you are experiencing.
The solution is to turn the JSON string of phone
to a dict
first using json.loads
), before you convert merged
to JSON.
import json
merged = {
"data": {
"id" : 1,
"user" : "chris",
"phone": "{\"device\": \"a\", \"brand\": \"a\"}"
}
}
merged['data']['phone'] = json.loads(merged['data']['phone'])
print(json.dumps(merged, indent=4))
Output:
{
"data": {
"id": 1,
"user": "chris",
"phone": {
"device": "a",
"brand": "a"
}
}
}
Of course, this assumes that phone
is always valid JSON, otherwise json.loads
will fail.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.