Search code examples
pythonjson

Extracting Text from Json Geocodio API


I'm using the Geocodio API to find congreessional district information for a given address. Here's my code:

    from geocodio import GeocodioClient
import json


client = GeocodioClient('MY API KEY')

availible_offices = ['Congress', 'State Legislature']
# offices_dict = ['cd': "Congress",'stateleg': "State Legislature"]

location = str(client.geocode("100 Renaissance Center, Detroit, MI 48243",fields=["cd", "stateleg"]))
print(location)

json.loads(location)

Here's the json it prints:

{'input': {'address_components': {'number': '100', 'street': 'Renaissance', 'suffix': 'Ctr', 'formatted_street': 'Renaissance Ctr', 'city': 'Detroit', 'state': 'MI', 'zip': '48243', 'country': 'US'}, 'formatted_address': '100 Renaissance Ctr, Detroit, MI 48243'}, 'results': [{'address_components': {'number': '100', 'street': 'Renaissance', 'suffix': 'Ctr', 'formatted_street': 'Renaissance Ctr', 'city': 'Detroit', 'county': 'Wayne County', 'state': 'MI', 'zip': '48243', 'country': 'US'}, 'formatted_address': '100 Renaissance Ctr, Detroit, MI 48243', 'location': {'lat': 42.329033, 'lng': -83.039762}, 'accuracy': 1, 'accuracy_type': 'rooftop', 'source': 'Semcog', 'fields': {'congressional_districts': [{'name': 'Congressional District 14', 'district_number': 14, 'congress_number': '117th', 'congress_years': '2021-2023', 'proportion': 1, 'current_legislators': [{'type': 'representative', 'bio': {'last_name': 'Lawrence', 'first_name': 'Brenda', 'birthday': '1954-10-18', 'gender': 'F', 'party': 'Democrat'}, 'contact': {'url': 'https://lawrence.house.gov', 'address': '2463 Rayburn House Office Building Washington DC 20515-2214', 'phone': '202-225-5802', 'contact_form': None}, 'social': {'rss_url': None, 'twitter': 'RepLawrence', 'facebook': '395759603917487', 'youtube': None, 'youtube_id': 'UCf0USy9GNigkB8O9sS1dodw'}, 'references': {'bioguide_id': 'L000581', 'thomas_id': '02252', 'opensecrets_id': 'N00034068', 'lis_id': None, 'cspan_id': '79924', 'govtrack_id': '412638', 'votesmart_id': '78851', 'ballotpedia_id': 'Brenda Lawrence (Michigan)', 'washington_post_id': None, 'icpsr_id': '21530', 'wikipedia_id': 'Brenda Lawrence'}, 'source': 'Legislator data is originally collected and aggregated by https://github.com/unitedstates/'}, {'type': 'senator', 'bio': {'last_name': 'Stabenow', 'first_name': 'Debbie', 'birthday': '1950-04-29', 'gender': 'F', 'party': 'Democrat'}, 'contact': {'url': 'https://www.stabenow.senate.gov', 'address': '731 Hart Senate Office Building Washington DC 20510', 'phone': '202-224-4822', 'contact_form': 'https://www.stabenow.senate.gov/contact'}, 'social': {'rss_url': 'http://stabenow.senate.gov/rss/?p=news', 'twitter': 'SenStabenow', 'facebook': 'SenatorStabenow', 'youtube': 'senatorstabenow', 'youtube_id': 'UCFoDKCvxSwCUfDv-4Eg4K5A'}, 'references': {'bioguide_id': 'S000770', 'thomas_id': '01531', 'opensecrets_id': 'N00004118', 'lis_id': 'S284', 'cspan_id': '45451', 'govtrack_id': '300093', 'votesmart_id': '515', 'ballotpedia_id': 'Debbie Stabenow', 'washington_post_id': None, 'icpsr_id': '29732', 'wikipedia_id': 'Debbie Stabenow'}, 'source': 'Legislator data is originally collected and aggregated by https://github.com/unitedstates/'}, {'type': 'senator', 'bio': {'last_name': 'Peters', 'first_name': 'Gary', 'birthday': '1958-12-01', 'gender': 'M', 'party': 'Democrat'}, 'contact': {'url': 'https://www.peters.senate.gov', 'address': '724 Hart Senate Office Building Washington DC 20510', 'phone': '202-224-6221', 'contact_form': 'https://www.peters.senate.gov/contact/email-gary'}, 'social': {'rss_url': None, 'twitter': 'SenGaryPeters', 'facebook': 'SenGaryPeters', 'youtube': 'RepGaryPeters', 'youtube_id': 'UC7LYNbnKSK2VZqQ98YROWHQ'}, 'references': {'bioguide_id': 'P000595', 'thomas_id': '01929', 'opensecrets_id': 'N00029277', 'lis_id': 'S380', 'cspan_id': '50199', 'govtrack_id': '412305', 'votesmart_id': '8749', 'ballotpedia_id': 'Gary Peters', 'washington_post_id': None, 'icpsr_id': '20923', 'wikipedia_id': 'Gary Peters'}, 'source': 'Legislator data is originally collected and aggregated by https://github.com/unitedstates/'}]}], 'state_legislative_districts': {'house': {'name': 'State House District 6', 'district_number': '6', 'is_upcoming_state_legislative_district': False}, 'senate': {'name': 'State Senate District 1', 'district_number': '1', 'is_upcoming_state_legislative_district': False}}}}]}

Here's the error that I get:

Traceback (most recent call last):
  File "/Users/aliallam/Desktop/APCSP/Create Task/Main.py", line 13, in <module>
    json.loads(location)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

I want to extract certain parts of the json and print them. Why am I getting this error and how do I go about doing that?

Thanks in advance!


Solution

  • Use eval():

    dataj = eval(location)
    dataj.keys()
    

    Output:

    dict_keys(['input', 'results'])