【问题标题】:Python normalize deeply nested JSONPython 规范化深度嵌套的 JSON
【发布时间】: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):

  1. cmd.success | cmd.params.count | cmd.params.result
  2. 真 | 37 | [{'id': '5f11c47fb2157c65ba029d4a', 'orgId': '5d0a54c6b2157c522d409098', 'name': ...以上 JSON 的其余部分}

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

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

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 2020-03-05
      • 2019-09-04
      • 2020-11-29
      • 1970-01-01
      • 2021-10-04
      • 2021-05-22
      • 1970-01-01
      • 2020-06-21
      相关资源
      最近更新 更多