You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pd.json_normalize指定meta参数时遇'Key not found'错误的解决方法

问题:如何正确使用pd.json_normalize指定meta路径获取嵌套字段

初始场景问题

以下代码可正常运行:

data = {
    "results": [
        {
            "name": 'record1',
            "values": [
                {
                    "key_1": "test_1",
                    "value": "1000"
                },
                {
                    "key_1": "test_2",
                    "value": "1001"
                }
            ]
        }
    ]
}

df = pd.json_normalize(data, record_path=["results","values"],meta=["results"])
display(df)

但仅修改meta路径加入name属性后,运行报错"Key 'name' not found":

data = {
    "results": [
        {
            "name": 'record1',
            "values": [
                {
                    "key_1": "test_1",
                    "value": "1000"
                },
                {
                    "key_1": "test_2",
                    "value": "1001"
                }
            ]
        }
    ]
}

df = pd.json_normalize(data, record_path=["results","values"],meta=["results","name"])
display(df)

需求是获取包含key_1、value、results.name三列的DataFrame,需修正meta路径的写法。

生产环境场景问题

后续测试生产环境JSON时同样报错Key 'name' not found:

{ 
    "createdAt": "2020-12-20T13:37:33.647Z",                        
    "completedAt": "2020-12-20T13:37:33.647Z",                            
    "header": { 
        "runId": "4c8f8516-cc55-4974-8856-9d2ea59d9aad", 
        "requestId": "2c8f8516-cc55-4974-8856-9d2ea59d9aae", 
        "metaData": {                                                
            "contentType": "pdf",
            "attachmentId": "3933"                       
        }
    }, 
    "body": {
        "documentMetadata": { 
            "documentSize": 505,                                    
            "pageCount": 23                                         
        },
        "results": [
            {                                                       
                "type": "table",           
                "tableName": "table_1",               
                "rows": [                                           
                    { 
                        "name:": "id_1",           
                        "values": [                                  
                            { 
                                "name": "field_1",          
                                "value": "1001"                       
                            },             
                            { 
                                "name": "field_2",          
                                "value": "1002"
                            }
                        ]
                    },
                    { 
                        "name:": "id_2", 
                        "values": [ 
                            { 
                                "name": "field_1",          
                                "value": "1001"                       
                            },             
                            { 
                                "name": "field_2",          
                                "value": "1002"
                            } 
                        ]
                    }  
                ] 
            }
        ]
    } 
}

执行代码:

df = pd.json_normalize(data, record_path=["body","results","rows","values"],meta=[["body","results","rows","name"]])

解决方案说明

  1. 初始场景修正:
    meta路径需要用嵌套列表表示从数组元素中提取字段。原写法["results","name"]会被解析为顶层results数组下直接找name,但results是数组,没有该字段。正确写法是将路径包裹在列表中:

    df = pd.json_normalize(data, record_path=["results","values"], meta=[["results", "name"]])
    

    运行后即可得到包含key_1、value、results.name三列的结果。

  2. 生产环境问题修正:
    经排查,生产环境JSON存在语法错误:"name:"多了一个冒号,应改为"name"。修正JSON后,使用meta=[["body","results","rows","name"]]即可正常运行。

内容的提问来源于stack exchange,提问作者Steve Just

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 21:13:29