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列,那麼它們的工作情況就會很好。