【发布时间】:2021-07-22 17:47:22
【问题描述】:
我需要使用 pandas 模块在 python 中解析和展平嵌套的 json。
源 JSON:
{
"people": [{
"name": "ABC",
"age": "33",
"mobile": "44545",
"location": "hyderabad",
"interests": [{
"hobby": "dancing",
"food": "continental",
"city": "Paris"
}]
},
{
"name": "DEF",
"age": "11",
"mobile": "12121212",
"location": "pune",
"interests": [{
"hobby": "reading",
"food": "Pizza",
"city": "France"
}]
}
]
}
从上面的json源文件中,我需要得到两个不同的json文件,如下:
输出 JSON 1:
{"name": "ABC", "age": "33", "mobile": "44545", "location": "hyderabad"}
{"name": "DEF", "age": "11","mobile": "12121212", "location": "pune”}
输出 JSON 2:
{"name": "ABC”, ”interests_hobby”:”dancing”, “interests_food”:”continental”, “interests_city”:”Paris”}
{“name": "DEF”, ”interests_hobby”:”reading”, “interests_food”:”Pizza”, “interests_city”:”France”}
条件是我们必须使用python和pandas模块。(pd.json_normalize)
【问题讨论】:
-
到目前为止你尝试过什么?请发布您的代码