New at Python. I am trying to grab the data below. I import requests, seem to set things up correctly. When I run the following:
import requests
url = "https://api.fda.gov/drug/ndc.json?search=finished:true&limit=1"
response = requests.get(url)
data = response.json()
data
I get the following:
{‘meta’: {‘disclaimer’: ‘Do not rely on openFDA to make decisions regarding medical care. While we make every effort to ensure that data is accurate, you should assume all results are unvalidated. We may limit or otherwise restrict your access to the API in line with our Terms of Service.’, ‘terms’: ‘https://open.fda.gov/terms/’, ‘license’: ‘https://open.fda.gov/license/’, ‘last_updated’: ‘2022-04-29’, ‘results’: {‘skip’: 0, ‘limit’: 1, ‘total’: 112692}}, ‘results’: [{‘product_ndc’: ‘72572-740’, ‘generic_name’: ‘SODIUM BICARBONATE’, ‘labeler_name’: ‘Civica’, ‘brand_name’: ‘Sodium Bicarbonate’, ‘active_ingredients’: [{‘name’: ‘SODIUM BICARBONATE’, ‘strength’: ‘84 mg/mL’}], ‘finished’: True, ‘packaging’: [{‘package_ndc’: ‘72572-740-20’, ‘description’: ‘20 VIAL in 1 CARTON (72572-740-20) > 50 mL in 1 VIAL’, ‘marketing_start_date’: ‘20200301’, ‘sample’: False}], ‘listing_expiration_date’: ‘20221231’, ‘openfda’: {‘manufacturer_name’: [‘Civica’], ‘rxcui’: [‘1868486’], ‘spl_set_id’: [‘a10fdfbc-94a0-49ab-b26c-de1df042bc89’], ‘is_original_packager’: [True], ‘unii’: [‘8MDF5V39QO’]}, ‘marketing_category’: ‘ANDA’, ‘dosage_form’: ‘INJECTION, SOLUTION’, ‘spl_id’: ‘a10fdfbc-94a0-49ab-b26c-de1df042bc89’, ‘product_type’: ‘HUMAN PRESCRIPTION DRUG’, ‘route’: [‘INTRAVENOUS’], ‘marketing_start_date’: ‘20190801’, ‘product_id’: ‘72572-740_a10fdfbc-94a0-49ab-b26c-de1df042bc89’, ‘application_number’: ‘ANDA211091’, ‘brand_name_base’: ‘Sodium Bicarbonate’}]}
I am then trying to get all the data, I get an error " KeyError : ‘results’ " and I am not understanding why even after spending a couple days working on it. Here is the code:
import requests
ndc = []
offset = 0
limit = 100
while True:
print("-----")
url = f"https://api.fda.gov/drug/ndc.json?offset={offset}&limit={limit}"
print("Requesting", url)
response = requests.get(url)
data = response.json()
# Any NDCs left?
if len(data['results']) == 0:
# if not, exit loop
break
# if we did find NDCs, add them
# to our list and then move on to the
# next offset
ndc.extend(data['results'])
offset = offset + 100
I then get the error I mentioned above: KeyError : ‘results’
Any assistance is greatly appreciated. Thank you