0
当我使用ProPublica的API,我可以通过简单地通过终端获得115国会议员的名单:移调JSON嵌套转换为CSV
curl "https://api.propublica.org/congress/v1/115/senate/members.json" -H "X-API-Key: "MY_API_KEY"
我得到一个JSON响应,看起来像这样:
{
"status":"OK",
"copyright":" Copyright (c) 2017 Pro Publica Inc. All Rights Reserved.",
"results":[
{
"congress": "115",
"chamber": "Senate",
"num_results": 101,
"offset": 0,
"members": [
{
"id": "A000360",
"api_uri":"https://api.propublica.org/congress/v1/members/A000360.json",
"first_name": "Lamar",
"middle_name": null,
"last_name": "Alexander",
"date_of_birth": "1940-07-03",
"party": "R",
"leadership_role": null,
"twitter_account": "SenAlexander",
"facebook_account": "senatorlamaralexander",
"youtube_account": "lamaralexander",
"govtrack_id": "300002",
"cspan_id": "5",
"votesmart_id": "15691",
"icpsr_id": "40304",
"crp_id": "N00009888",
"google_entity_id": "/m/01rbs3",
"url": "https://www.alexander.senate.gov/public",
"rss_url": "https://www.alexander.senate.gov/public/?a=RSS.Feed",
"contact_form": "http://www.alexander.senate.gov/public/index.cfm?p=Email",
"domain": null,
"in_office": true,
"dw_nominate": 0.323,
"ideal_point": null,
"seniority": "15",
"next_election": "2020",
"total_votes": 187,
"missed_votes": 7,
"total_present": 0,
"ocd_id": "ocd-division/country:us/state:tn",
"office": "455 Dirksen Senate Office Building",
"phone": "202-224-4944",
"fax": "202-228-3398",
"state": "TN",
"senate_class": "2",
"state_rank": "senior",
"lis_id": "S289"
,"missed_votes_pct": 3.74,
"votes_with_party_pct": 98.89
},
{
"id": "B000575",
"api_uri":"https://api.propublica.org/congress/v1/members/B000575.json",
"first_name": "Roy",
"middle_name": null,
"last_name": "Blunt",
"date_of_birth": "1950-01-10",
"party": "R",
"leadership_role": null,
"twitter_account": "RoyBlunt",
"facebook_account": "SenatorBlunt",
"youtube_account": "SenatorBlunt",
"govtrack_id": "400034",
"cspan_id": "45465",
"votesmart_id": "418",
"icpsr_id": "29735",
"crp_id": "N00005195",
"google_entity_id": "/m/034fn4",
"url": "https://www.blunt.senate.gov/public",
"rss_url": "http://www.blunt.senate.gov/public/?a=RSS.Feed",
"contact_form": "https://www.blunt.senate.gov/public/index.cfm/contact-roy",
"domain": null,
"in_office": true,
"dw_nominate": 0.431,
"ideal_point": null,
"seniority": "7",
"next_election": "2022",
"total_votes": 187,
"missed_votes": 2,
"total_present": 0,
"ocd_id": "ocd-division/country:us/state:mo",
"office": "260 Russell Senate Office Building",
"phone": "202-224-5721",
"fax": "202-224-8149",
"state": "MO",
"senate_class": "3",
"state_rank": "junior",
"lis_id": "S342"
,"missed_votes_pct": 1.07,
"votes_with_party_pct": 99.46
},
等等
但是,当我将其转换为CSV,这只是两行(其中一个之中列标题),绵延近4000列。看起来像JSON嵌套的方式,这是我可以将其转换为CSV的唯一方法。希望它转换为CSV,以便我可以正确导入SQL。
我计算了标题,每个会员有39个。他们被制定为成员/ 0/id,会员/ 0/api_url,等到成员/ 100/id,会员/ 100/api_url等。
有反正我可以做到这一点,无需手动改变?在理想的世界中,我可以运行我的终端脚本,输出为CSV,然后上传到SQL中使用它。如果结果是100行,39列,而不是1行和3900列,那么它们的工作情况就会很好。