英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论