json_normalize不会读取所有数据

json_normalize不会读取所有数据,json,pandas,Json,Pandas,我有一个json文件,我想将所有信息展平并检索到一个数据帧中。json文件如下所示: jsonstr = { "calculation": { "id": "3k3k3k3kwk3kwk", "Id": 23, "submissionDate": 1622428064679, "serverVersion": "3.3.5.6.r&

我有一个
json
文件,我想将所有信息展平并检索到一个数据帧中。
json
文件如下所示:

jsonstr = {
  "calculation": {
    "id": "3k3k3k3kwk3kwk",
    "Id": 23,
    "submissionDate": 1622428064679,
    "serverVersion": "3.3.5.6.r",
    "tag": [
      {
        "code": "qq4059331155113278",
        "manual": {
          "location": {
            "x": 26.5717,
            "y": 59.4313,
            "z": 0.0,
            "floor": 0
          },
          "timestamp": 1599486138000
        },
        "device": null,
        "measurements": [
          {
            "Address": "D_333",
            "subcell": "",
            "frequency": 14.0,
            "dfId": 0
          },
          {
            "trxAddress": "D_334",
            "subcell": "",
            "frequency": 11.0,
            "dfId": 0
          }]
    }]
}
}
现在,像往常一样,我做以下事情。我认为这将返回所有的“字段”,包括
id
id
submissionDate
等等

import os, json
import pandas as pd
import numpy as np
import glob
pd.set_option('display.max_columns', None)

file = './Testjson.json'
#file = './jsondumps/ff80818178f93bd90179ab51781e1c95.json'
with open(file) as json_string:
    jsonstr = json.load(json_string)

labels = pd.json_normalize(jsonstr, record_path=['calculation','tag'])
但事实上,它的回报是:

   code device  \
0  qq4059331155113278   None   

                                        measurements  manual.location.x  \
0  [{'Address': 'D_333', 'subcell': '', 'frequenc...            26.5717   

   manual.location.y  manual.location.z  manual.location.floor  \
0            59.4313                0.0                      0   

   manual.timestamp  
0     1599486138000
并尝试以下方法

labels = pd.json_normalize(jsonstr, record_path=['calculation','tag'], meta=['id', 'Id'])
返回一个错误:

KeyError: 'id'
这是有道理的。但我一开始做错了什么?为什么我不能获取
calculation
下的所有字段,因为它们位于路径中


非常适合任何见解

您的语法在元参数上有点偏离
id
id
位于数据帧的末尾

如果您希望展平整个json,请查看
展平\u json
。这是一个与嵌套json一起使用的非常好的库

pd.json_normalize(jsonstr, record_path=['calculation','tag'],  meta=[['calculation','id'],['calculation','Id']])

                 code device                                       measurements  manual.location.x  manual.location.y  manual.location.z  manual.location.floor  manual.timestamp  calculation.id calculation.Id
0  qq4059331155113278   null  [{'Address': 'D_333', 'subcell': '', 'frequenc...            26.5717            59.4313                0.0                      0     1599486138000  3k3k3k3kwk3kwk             23

你的语法在元参数上有点偏离
id
id
位于数据帧的末尾

如果您希望展平整个json,请查看
展平\u json
。这是一个与嵌套json一起使用的非常好的库

pd.json_normalize(jsonstr, record_path=['calculation','tag'],  meta=[['calculation','id'],['calculation','Id']])

                 code device                                       measurements  manual.location.x  manual.location.y  manual.location.z  manual.location.floor  manual.timestamp  calculation.id calculation.Id
0  qq4059331155113278   null  [{'Address': 'D_333', 'subcell': '', 'frequenc...            26.5717            59.4313                0.0                      0     1599486138000  3k3k3k3kwk3kwk             23

flatte_json
上:我在整个
json
上尝试了这个方法,但由于某种原因,它失败了,错误是
无法分配5.18 TB的内存(或类似的东西)。对文件的各个部分执行此操作时效果良好。这就是为什么我需要这些ID,这样我就可以在它们上面合并。很高兴你让它工作起来了。在
flatte_json
上:我在整个
json
上尝试了这一点,但由于某些奇怪的原因,它失败了,错误是
无法分配5.18 TB的内存(或类似的东西)。对文件的各个部分执行此操作时效果良好。这就是为什么我需要这些ID,这样我就可以在它们上面合并。很高兴你能让它工作。