我正在尝试通过在熊猫中找到的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)如下所示:
(1,3)单元格包含文本中其余的JSON。我正在寻找的期望输出将是一列,进一步深入到JSON。例如,cmd.params.result.id和JSON对象中的包含ID。
看来,我的JSON格式设置不允许其进一步深入。 JSON Normalize Documentation有一个meta和record_path参数,但是我一直无法使其正常工作。任何帮助将不胜感激!
答案 0 :(得分:0)
您可以使用以下内容访问2个测试对象的嵌套级别:
df_norm = json_normalize(data=Response_JSON, record_path=['cmd', 'params', 'result'])
...使用print(df_norm.to_string())
打印以下内容:
id orgId name desc eventType status ts templateGroup templateName templateId createdBy createdOn summary.ready summary.inProgress summary.success summary.failure summary.retry
0 5f11c47fb2157c65ba029d4a 5d0a54c6b2157c522d409098 tag test Removes unnecessary tags campaign.thing new 2020-07-17T15:32:15.894Z Tags Tag_Removal 5e84f5127094416efc422f67 tester 2020-07-17T15:32:15.894Z 0 0 0 0 0
1 5f11c414b2157c65ba016b35 5d0a54c6b2157c522d409098 tag update NaN campaign.thing new 2020-07-17T15:30:28.139Z Tags Tag_Add 5e84f2fe7094416efc3dd0cd tester 2020-07-17T15:30:28.139Z 0 0 0 0 0