Skip to content Skip to sidebar Skip to footer

How Can I Fix/ Workaround This Keyerror When I Try To Extract From A Json Via My Python Api Request?

I have been trying to seed a django DB with some covid data from an api and get a KeyError for a particular data type - in the source it is a floating_timstamp ('lab_report_date' :

Solution 1:

Because there's an item in the array response.json() that does not contain a key lab_report_date. That happens when the backend data is not so clean.

So what you need to do is to use try-except code block to handle this exception. The following code runs well now.

import requests
response = requests.get("https://data.cityofchicago.org/resource/naz8-j4nc.json")
print("The total length of response is %s" % len(response.json()))

report_date = []
for i in response.json():
    try:
        ls = i['lab_report_date']
        report_date.append(ls)
    except:
        print("There is an item in the response containing no key lab_report_date:")
        print(i)

print("The length of report_date is %s" % len(report_date))

The output of the above code is as follows.

The total length of response is592
There is an item in the response containing no key lab_report_date:
{'cases_total': '504', 'deaths_total': '1', 'hospitalizations_total': '654', 'cases_age_0_17': '28', 'cases_age_18_29': '116', 'cases_age_30_39': '105', 'cases_age_40_49': '83', 'cases_age_50_59': '72', 'cases_age_60_69': '61', 'cases_age_70_79': '25', 'cases_age_80_': '14', 'cases_age_unknown': '0', 'cases_female': '264', 'cases_male': '233', 'cases_unknown_gender': '7', 'cases_latinx': '122', 'cases_asian_non_latinx': '15', 'cases_black_non_latinx': '116', 'cases_white_non_latinx': '122', 'cases_other_non_latinx': '30', 'cases_unknown_race_eth': '99', 'deaths_0_17_yrs': '0', 'deaths_18_29_yrs': '0', 'deaths_30_39_yrs': '0', 'deaths_40_49_yrs': '1', 'deaths_50_59_yrs': '0', 'deaths_60_69_yrs': '0', 'deaths_70_79_yrs': '0', 'deaths_80_yrs': '0', 'deaths_unknown_age': '0', 'deaths_female': '0', 'deaths_male': '1', 'deaths_unknown_gender': '0', 'deaths_latinx': '0', 'deaths_asian_non_latinx': '0', 'deaths_black_non_latinx': '0', 'deaths_white_non_latinx': '1', 'deaths_other_non_latinx': '0', 'deaths_unknown_race_eth': '0', 'hospitalizations_age_0_17': '30', 'hospitalizations_age_18_29': '78', 'hospitalizations_age_30_39': '74', 'hospitalizations_age_40_49': '96', 'hospitalizations_age_50_59': '105', 'hospitalizations_age_60_69': '111', 'hospitalizations_age_70_79': '89', 'hospitalizations_age_80_': '71', 'hospitalizations_age_unknown': '0', 'hospitalizations_female': '310', 'hospitalizations_male': '341', 'hospitalizations_unknown_gender': '3', 'hospitalizations_latinx': '216', 'hospitalizations_asian_non_latinx': '48', 'hospitalizations_black_non_latinx': '208', 'hospitalizations_white_non_latinx': '135', 'hospitalizations_other_race_non_latinx': '31', 'hospitalizations_unknown_race_ethnicity': '16'}
The length of report_date is591

Solution 2:

You can use the dict get method to read the data from json response like below :-

report_date = []
for i in response.json():
    iftype(i) == dict:  # Just check the type to avoid the runtime error.ls = i.get('lab_report_date', None)
        ifls:
            report_date.append(ls)

Solution 3:

hi i have a similar issue which is sometimes the response comes empty from the api request which cause to me a stop in the Code Execution :

i found an easy solution for it now :

let's say you have a :

requestfromapi = requests.get("https://api-server")

if requestfromapi.json()['data']['something'] != KeyError:

   print(requestfromapi.json()['data']['something'])

// this will make sure that your code will not stop from executing .

Post a Comment for "How Can I Fix/ Workaround This Keyerror When I Try To Extract From A Json Via My Python Api Request?"