I've seen several related "nested json in Python" questions but the syntax for this Corona virus JSON data is giving me problems. Here's a sample:
{"recovered":524855,"list":[
{"countrycode":"US","country":"United States of America","state":"South Carolina","latitude":"34.22333378","longitude":"-82.46170658","confirmed":15228,"deaths":568},
{"countrycode":"US","country":"United States of America","state":"Louisiana","latitude":"30.2950649","longitude":"-92.41419698","confirmed":43612,"deaths":2957}
]}
If I just want to get to Louisiana, here's what I was trying:
import json
import requests
url = "https://covid19-data.p.api.com/us"
headers = {
'x-api-key': "<api-key>",
'x-api-host': "covid19-data.p.api.com"
}
response = requests.request("GET", url, headers=headers)
coronastats = json.loads(response.text)
la_deaths = coronastats["list"][0]["countrycode"]["US"]["country"]["United States of America"]["state"]["Louisiana"]["deaths"]
print("Value: %s" % la_deaths)
I get: "TypeError: string indices must be integers"
This is obviously a list (I'm a detective and deduced that a variable named "list" might be a list) but the long key-value list is throwing me off (I think).