指定json_normalize的meta参数时出现错误。

huangapple go评论77阅读模式
英文:

Error when specifying meta parameter of json_normalize

问题

The below code works as expected:

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)

But I get the following error when I try the below - 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)

The only difference is the inclusion of the "name" attribute in my meta path. What is the correct way to specify the meta path in this case? I am looking for a 3 column result with key_1, value, and results.name.

Update - Was able to get this to work with my sample JSON (see response below) but it is not working with my production JSON.

{ 
    "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"]])

Returns an error - Key 'name' not found.

Update 2 - There was an error in my production JSON. The first answer resolves my question. Sorry about that!

英文:

The below code works as expected

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)

But I get the following error when I try the below - "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)

The only difference is the inclusion of the "name" attribute in my meta path. What is the correct way to specify the meta path in this case? I am looking for a 3 column result with key_1, value, and results.name.

Update - Was able to get this to work with my sample JSON (see response below) but it is not working with my production JSON.

{ 
    "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"]])

Returns an error - Key 'name' not found.

Update 2 - There was an error in my production JSON. The first answer resolves my question. Sorry about that!

答案1

得分: 2

你可以通过以下方式修复错误:

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

因为 meta 需要相对于你正在规范化的数据的路径。错误是因为它尝试遍历路径 data["name"],而该路径不存在。

英文:

You can fix the error by using below:

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

as meta expects a path relative to the data that you are normalizing. The error is because it is trying to traverse the path data["name"] which does not exist.

huangapple
  • 本文由 发表于 2023年7月28日 02:30:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定