【发布时间】:2020-11-09 00:45:42
【问题描述】:
我正在尝试通过 pandas 中的 json_normalize 函数对从 API 收到的 JSON 进行规范化:
{
"cmd": {
"success": true,
"params": {
"count": 37,
"result": [
{
"id": "5f11c47fb2157c65ba029d4a",
"orgId": "5d0a54c6b2157c522d409098",
"name": "tag test",
"desc": "Removes unnecessary tags",
"eventType": "campaign.thing",
"status": "new",
"ts": "2020-07-17T15:32:15.894Z",
"summary": {
"ready": 0,
"inProgress": 0,
"success": 0,
"failure": 0,
"retry": 0
},
"emailUpdates": {},
"templateGroup": "Tags",
"templateName": "Tag_Removal",
"templateId": "5e84f5127094416efc422f67",
"createdBy": "tester",
"createdOn": "2020-07-17T15:32:15.894Z"
},
{
"id": "5f11c414b2157c65ba016b35",
"orgId": "5d0a54c6b2157c522d409098",
"name": "tag update",
"eventType": "campaign.thing",
"status": "new",
"ts": "2020-07-17T15:30:28.139Z",
"summary": {
"ready": 0,
"inProgress": 0,
"success": 0,
"failure": 0,
"retry": 0
},
"emailUpdates": {},
"templateGroup": "Tags",
"templateName": "Tag_Add",
"templateId": "5e84f2fe7094416efc3dd0cd",
"createdBy": "tester",
"createdOn": "2020-07-17T15:30:28.139Z"
},
...display another 35 JSON objects
]
}
}
}
收到回复后,我会尝试通过下面的 python 行规范化我的代码:
df_norm = pd.json_normalize(data=Response_JSON, record_path='cmd')
我的输出不是我想要的,因为我最终制作了如下所示的数据框大小 (1,3):
- cmd.success | cmd.params.count | cmd.params.result
- 真 | 37 | [{'id': '5f11c47fb2157c65ba029d4a', 'orgId': '5d0a54c6b2157c522d409098', 'name': ...以上 JSON 的其余部分}
(1,3) 单元格包含 JSON 文本的其余部分。我正在寻找的所需输出将是进一步深入 JSON 的列。例如,cmd.params.result.id 和 JSON 对象中包含的 id。
我的 JSON 格式似乎不允许它进一步深入研究。 JSON Normalize Documentation 有一个 meta 和 record_path 参数,但我没有成功让它工作。任何帮助将不胜感激!
【问题讨论】: