我正在尝试normalize一个非常嵌套的json,但似乎无法正常工作。

这是杰森

  "results": [
    {
      "group": {
        "mediaType": "mail"
      },
      "data": [
        {
          "interval": "2020-01-20T23:00:00.000Z/2020-01-21T23:00:00.000Z",
          "metrics": [
            {
              "metric": "nOf",
              "stats": {
                "count": 31
              }
            },
            {
              "metric": "tHa",
              "stats": {
                "max": 4115605,
                "min": 47151,
                "count": 30,
                "sum": 36606219
              }
            }
          ]
        }
      ]
    }


我尝试过的是

from pandas.io.json import json_normalize

data = json_normalize(response_json, 'results', ['mediaType', 'interval', ['metrics', 'metric', ['stats']]])


我要获取的是按mediaType分组的df,并查看类似nOf.counttHa.max等的统计信息。

提前致谢!

最佳答案

仅供参考,我使用此功能解决了

def flatten_json(y):
    out = {}

    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x

    flatten(y)
    return out

flat = flatten_json(response_json)
normalized_df = json_normalize(flat)

10-04 11:24