Python规范化深度嵌套的JSON

2024-04-27 00:01:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过在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):

  1. cmd.success | cmd.params.count | cmd.params.result
  2. True | 37 |[{'id':'5f11c47fb215c65ba029d4a','orgId':'5d0a54c6b215c52d409098','name':…上面JSON的其余部分]

(1,3)单元格包含文本形式的JSON的其余部分。我正在寻找的所需输出将是进一步深入JSON的列。例如,cmd.params.result.id和JSON对象的包含id

看来我的JSON的格式不允许它进一步深入。JSON Normalize Documentation有一个meta和record_path参数,但我没有成功地让它工作。任何帮助都将不胜感激


Tags: namecmdidjsontagcountparamsresult
1条回答
网友
1楼 · 发布于 2024-04-27 00:01:07

对于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

相关问题 更多 >