You need to enable JavaScript to run this app. 使用pandas的json_normalize无法读取JSON中calculation下全部数据如何解决
优惠活动
大模型
产品
解决方案
定价
更多

使用pandas的json_normalize无法读取JSON中calculation下全部数据如何解决

错误原因

pd.json_normalize的meta参数默认从JSON根节点开始匹配字段路径,你需要提取的id、Id等字段都属于calculation节点的子属性,不是根节点的直接属性,因此直接写meta=['id', 'Id']无法匹配到对应字段,触发KeyError。
同时record_path=['calculation','tag']仅指定了将tag数组的每个元素作为DataFrame的一行,只会自动展开tag节点下的字段,上层节点的字段不会自动合并,必须通过meta主动指定路径才能引入。

解决方法

场景1:保留tag为行粒度,仅引入calculation层级公共字段

将meta参数的字段路径补全到对应层级即可:

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

场景2:完全展开所有层级,以measurements单条记录为行粒度

如果需要把measurements数组也展开为单独行,调整record_path并补充对应上层字段的meta路径即可:

labels = pd.json_normalize(
    jsonstr, 
    record_path=['calculation','tag', 'measurements'], 
    meta=[
        ['calculation', 'id'],
        ['calculation', 'Id'],
        ['calculation', 'submissionDate'],
        ['calculation', 'serverVersion'],
        ['calculation', 'tag', 'code'],
        ['calculation', 'tag', 'device'],
        ['calculation', 'tag', 'manual.location.x'],
        ['calculation', 'tag', 'manual.location.y'],
        ['calculation', 'tag', 'manual.location.z'],
        ['calculation', 'tag', 'manual.location.floor'],
        ['calculation', 'tag', 'manual.timestamp']
    ]
)

内容的提问来源于stack exchange,提问作者Serge de Gosson de Varennes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 12:06:04