我一整天都在努力解决这个问题,我想转向一个 csv。
它代表隶属于英国公司大楼 API 中编号为“OC418979”的公司的官员。
我已经将 json 截断为在“项目”中仅包含 2 个对象。
我想得到的是这样的csv
OC418979, country_of_residence, officer_role, appointed_on
OC418979, country_of_residence, officer_role, appointed_on
OC418979, country_of_residence, officer_role, appointed_on
OC418979, country_of_residence, officer_role, appointed_on
...
有两个额外的复杂性:有两种类型的“官员”,有些是人,有些是公司,所以并非所有关键人物都出现在另一个人身上,反之亦然。我希望这些条目为“空”。第二个复杂性是那些嵌套对象,如“名称”,其中包含一个逗号!或地址,其中包含几个子对象(我想我可以在 pandas 中展平)。
{
"total_results": 13,
"resigned_count": 9,
"links": {
"self": "/company/OC418979/officers"
},
"items_per_page": 35,
"etag": "bc7955679916b089445c9dfb4bc597aa0daaf17d",
"kind": "officer-list",
"active_count": 4,
"inactive_count": 0,
"start_index": 0,
"items": [
{
"officer_role": "llp-designated-member",
"name": "BARRICK, David James",
"date_of_birth": {
"year": 1984,
"month": 1
},
"appointed_on": "2017-09-15",
"country_of_residence": "England",
"address": {
"country": "United Kingdom",
"address_line_1": "Old Gloucester Street",
"locality": "London",
"premises": "27",
"postal_code": "WC1N 3AX"
},
"links": {
"officer": {
"appointments": "/officers/d_PT9xVxze6rpzYwkN_6b7og9-k/appointments"
}
}
},
{
"links": {
"officer": {
"appointments": "/officers/M2Ndc7ZjpyrjzCXdFZyFsykJn-U/appointments"
}
},
"address": {
"locality": "Tadcaster",
"country": "United Kingdom",
"address_line_1": "Westgate",
"postal_code": "LS24 9AB",
"premises": "5a"
},
"identification": {
"legal_authority": "UK",
"identification_type": "non-eea",
"legal_form": "UK"
},
"name": "PREMIER DRIVER LIMITED",
"officer_role": "corporate-llp-designated-member",
"appointed_on": "2017-09-15"
}
]
}
我一直在做的是创建新的 json 对象来提取我需要的字段,如下所示:
{officer_address:.items[]?.address, appointed_on:.items[]?.appointed_on, country_of_residence:.items[]?.country_of_residence, officer_role:.items[]?.officer_role, officer_dob:items.date_of_birth, officer_nationality:.items[]?.nationality, officer_occupation:.items[]?.occupation}
但是查询运行了几个小时——我相信有更快的方法。
现在我正在尝试这种新方法 - 创建一个以公司编号为根的 json,并将其官员列表作为参数。
{(.links.self | split("/")[2]): .items[]}