【发布时间】:2023-03-23 01:54:01
【问题描述】:
我正在尝试解析 JSON 文件并将其导出为将单独处理的 CSV。
我的 JSON 看起来像这样:
"data": [
{
"id": "fHF6fFQrZ8Qxwrw1ALiMC5_a2920fb2-e91b-11e9-9b3f-2fe890320230",
"type": "leads",
"attributes": {
"facebook_url": "someurl",
"status": "new",
"twitter_handle": "somehandle",
"first_visit_date": "2020-06-01",
"last_visit_date": "2020-06-01",
"linkedin_url": "linkedinlink",
"name": "name",
"website_url": "website",
"industry": "Computer Software",
"phone": "123123123",
"crm_lead_id": null,
"crm_organization_id": null,
"employee_count": 10001,
"tags": [],
"logo_url": "logo"
},
"relationships": {
"location": {
"data": {
"id": "a2920fb2-e91b-11e9-9b3f-2fe890320230",
"type": "locations"
}
}
}
}
我使用 pandas 来规范化数据:
with open('leads.json') as data_file:
d= json.load(data_file)
df = json_normalize(d['data'])
这导致列名被命名为:
id type attributes.facebook_url attributes.status attributes.twitter_handle attributes.first_visit_date
我应该如何只获取列名而不附加整个属性路径?
edit:我不会解析关系节点。
示例输出为:
id type facebook_url
1 sometype http://myurl
【问题讨论】:
-
json_normalize(d['data']['attributes']) ?
-
您可以从示例数据中添加预期输出吗?因为属性是为了区分相同的值,比如
id,存在于内部和输出json部分中 -
如上所述,你会得到重复的列,添加你的目标输出并阅读minimal reproducible example
-
添加了示例输出。
-
您的 JSON 不是有效的 json,如果您可以提供一个涵盖整个案例的输出,这样会更容易理解。
标签: python json python-3.x pandas