Error - list index: got string, want int

Probably not the best way to learn about coding. I was able to finally get the development space up and running, which was an adventure for the first-timer.

I am working on learning how to modify existing examples to make my own apps. The first step is using a COVID API (Covid Act Now) to pull the test positivity rate. I took the bitcoin example and tried to modify it to work. But getting an error code.

load(“”, “render”)
load(“”, “http”)
load(“encoding/”, “base64”)
load(“”, “cache”)


def main():
rep = http.get(US_COUNTY_COVID)
if rep.status_code != 200:
fail(“Covid request failed with status %d”, rep.status_code)

rate = rep.json()["US"]["WA"]["King County"]["testPositivityRatio"]

return render.Root(
    child = render.Text("%d Cases" % rate)

Ironically I found a walk-through using a COVID API from India that I was able to get to work but struggling here as I dive into this more. Any insight on why this is throwing me the “got string, want int” error?

you aren’t indexing the json correctly.

try using a json reader to help parse through it, i like this online viewer here. You just need to copy and paste the json text and then use the viewer.

Your entry isn’t until 3050…

so you would need `rate=rep.json()[3050][“metrics”][“riskLevels”][“testPositivityRatio”] should get you a value of 4.

Also this json is big, would see if you can also specify some type of filter to get a smaller json when calling the data.

edit: spelling, also reading up on how dicts work may be helpful as you work through this. But the error you got basically tells you you have a vector not a dict. I think type(x) should return the type of your variable too.

edit2: you also just exposed your api key in that link in your original post. may want to get a new one and make that one invalid if you can.

Thank you for the callouts! I definitely have a lot to read up on. Cleared out the API above and scrapped it to snag a new one and hopefully find a more filtered-down one to work with. Going to work through the suggestions above and update.

Thanks again

No problem. and honestly for me I find learning to code is easiest by doing, but sounds like you’re taking the right steps.

The json viewer really helps though in looking at what information, but how it’s all arranged too relatively quickly. I use it often.

As for filtering, you may be able to specify/search by state? sometimes urls have extra parameters you can pass to the url. Likely though there are different endpoints. In fact, the CovidActNow getting started guide says you should be able to query all counties for a single state. so you may be able to have a user specify a state/county and search more refined depending on your use case.

Thanks again pineapple! I was able to get it to work, by just removing state/county and just went with 3050 and cdctransmissionrate.

Now down to playing with a few more items, filtering the data, just in general playing wiht it more .

It is actually how I cant get through html (thanks myspace days) and write sql from work. Start off with pulling other code, modify and see how it impacts the output and keep playing. Hopefully I will get to the point of building out some apps that are a bit more intense. My partner really wants one that can present currently migrating animals in our area or river’s flow reports. Really glad I got a tidbyt to have fun with like this.

Glad you got it to work! And agreed, it is definitely more fun to code something that you will actually use and want for information/entertainment purposes. glad to see you got it to work!

If you plan on putting it on the community repo, I would suggest using the different endpoint that allows you to specify at least a state and county or maybe uses location to find the county or something?

Also the index in some cases could change (doubt they’ll be adding a new county soon, but it’s possible), which is why doing it by index may not be the best long-term solution IF it can be avoided (had that happen before with sports stuff because they added extra rows). Ended up having to check the name of each index for the stat name instead to make sure I got the right one.

An app to tell you what animals are migrating would be really cool to have on the tidbyt!