pretty new to both stack overflow and python so my apologies if the formatting is wrong. Not sure if necessary but I am using VS Code.
I am pulling data from a covid-19 rapid api and it outputs the data (json) but doesn't give me the data I actually need:
This is the code to pull it:
import requests
url = "https://covid-19-data.p.rapidapi.com/report/country/code"
querystring = {"date":"2020-04-01","code":"it"}
headers = { 'x-rapidapi-host': "covid-19-data.p.rapidapi.com", 'x-rapidapi-key': "SIGN-UP-FOR-KEY" }
response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
Once I get that, it outputs:
[{"country":"USA","provinces":[{"province":"USA"}],"latitude":37.09024,"longitude":-95.712891,"date":"2022-01-24"}]
However, on the rapid api website, it shows under 'Example Responses' that the data I need (deaths, recovered, active, etc.) is nested under the [{"province":"USA"}] section:
[1 item
0:{5 items
"country":"Italy"
"provinces":[1 item
0:{5 items
"province":"Italy"
"confirmed":110574
"recovered":16847
"deaths":13155
"active":80572
}
]
"latitude":41.87194
"longitude":12.56738
"date":"2020-04-01"
}
]
I want to pull the data from that nested dictionary but I am not sure how. This is my attempt which hasn't worked but just wanted to show the data:
import requests
import json
def get_covid_data_19(country_code, date):
'''
This function gets COVID 19 data via rapid API. This takes two parameters:
Country Name (string) and Date (YYYY-MM-DD)
'''
c = country_code
d = date
# Endpoint that we are going to be hitting:
url = "https://covid-19-data.p.rapidapi.com/report/country/code"
querystring = {"code" : c, "date" : d}
headers = {
'x-rapidapi-host': "covid-19-data.p.rapidapi.com",
'x-rapidapi-key': "insertkeyhere"
}
response = requests.request("GET", url, headers=headers, params=querystring)
json_data = response.json()
data = []
for response in json_data:
data.append ({
'confirmed' : response.get('confirmed'),
'recovered' : response.get('recovered'),
'deaths' : response.get('deaths'),
'active' : response.get('active'),
'date' : response.get('date'),
})
print(data)
I also attemped to retrieve the data as xml format and could not figure out how to expand the array either. Any advice would be awesome. Thanks.