[英]Python Converting Nested JSON with Array of Objects, and Objects to Excel
Can someone help me in converting the below JSON file to Excel?有人可以帮我将以下 JSON 文件转换为 Excel 吗?
Excel Output format Excel Output 格式
metaData.user report.from report.to lossInfo.locationName.Name lossInfo.locationName.locNbr lossInfo.locationDetails.locNm lossInfo.locationDetails.locAddress.locCity metaData.user report.from report.to lossInfo.locationName.Name lossInfo.locationName.locNbr lossInfo.locationDetails.locNm lossInfo.locationDetails.locAddress.locCity
Test User 12-12-2021 12-12-2022 test1 12 xyz abc测试用户 12-12-2021 12-12-2022 test1 12 xyz abc
Test User 12-12-2021 12-12-2022 test2 121 xyz1 abc1测试用户 12-12-2021 12-12-2022 test2 121 xyz1 abc1
{
"metaData": {
"formName": "A1",
"user": "Test User"
},
"report": {
"from": "12/12/2021",
"to": "12/12/2022"
},
"lossInfo": [
{
"locationName": {
"name": "test1",
"locNbr": "12"
},
"locationDetails": {
"locNm": "xyz",
"locAddress": {
"locCity": "abc",
"locStateCd": "abcd"
},
"state": "ab",
"lossLocation": "cd"
}
},
{
"locationName": {
"name": "test11",
"locNbr": "121"
},
"locationDetails": {
"locNm": "xyz1",
"locAddress": {
"locCity": "abc1",
"locStateCd": "abcd1"
},
"state": "ab1",
"lossLocation": "cd1"
}
}
]
}
The json format you gave is wrong.你给的json格式不对。 There is no comma before the key named locationDetails .
名为locationDetails的键前没有逗号。 You can use json_normalize after you fix it.
修复后可以使用json_normalize 。
df = pd.json_normalize(json_data,meta=['lossInfo']).explode('lossInfo').reset_index(drop=True)
df = df.join(pd.json_normalize(df.pop('lossInfo')))
'''
| | metaData.formName | metaData.user | report.from | report.to | locationName.name | locationName.locNbr | locationDetails.locNm | locationDetails.locAddress.locCity | locationDetails.locAddress.locStateCd | locationDetails.state | locationDetails.lossLocation |
|---:|:--------------------|:----------------|:--------------|:------------|:--------------------|----------------------:|:------------------------|:-------------------------------------|:----------------------------------------|:------------------------|:-------------------------------|
| 0 | A1 | Test User | 12/12/2021 | 12/12/2022 | test1 | 12 | xyz | abc | abcd | ab | cd |
| 1 | A1 | Test User | 12/12/2021 | 12/12/2022 | test11 | 121 | xyz1 | abc1 | abcd1 | ab1 | cd1 |
'''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.