Trouble with Zenquote.io Response?

I used the Bitcoin sample app as a foundation for a Quote of the Day App.

Getting the following run time error when attempting to index the resulting JSON response

Error: list index: got string, want int (referring to quote = rep.json()[“q”] )

JSON is pasted below the code. Does the HTML block quote escape sequence somehow screw up the indexing? I compared it to the raw JSON of the Bitcoin example and the only difference in formatting are the blockquote HTML commands. It still looks like a legit string however.

ZENQUOTE_RANDOM_URL = “https://zenquotes.io/api/random
def main():
quote_cached = cache.get(“quote_rate”)
print(“Quote Cached: %s” % quote_cached)
if quote_cached != None:
print(“Fetching Cached Quote”)
quote = (quote_cached)
else:
print(“Fetching from Zenquote”)
rep = http.get(ZENQUOTE_RANDOM_URL)
print (“Rep: %s” % rep)
if rep.status_code != 200:
fail(“Zenquote request failed with status %d”, rep.status_code)
quote = rep.json()[“q”]
print (“quote: %s” % quote)
cache.set(“quote_rate”, quote, ttl_seconds=30)

	return render.Root(
child = render.Text("Quote: %s" % quote)
 	)

JSON as follows:
{“a”: “Napoleon Hill”, “h”: “

“Every adversity, every failure, every heartbreak, carries with it the seed of an equal or greater benefit.” — Napoleon Hill
”, “q”: “Every adversity, every failure, every heartbreak, carries with it the seed of an equal or greater benefit.”}

Looks like great minds think alike! Daily quote app was exactly the first thing I made :slightly_smiling_face:

The issue is that the format is slightly different between the bitcoin example api response and the zenquote one.

https://zenquotes.io/api/random
results in a list containing the actual json you’re interested in whereas

https://api.coindesk.com/v1/bpi/currentprice.json
results in the direct json string

The error message itself is saying that it sees a list and you’re using a character to index into the list, which it’s confused about. Instead, you can unwrap the list by indexing the first element. Here’s a snippet of what I did:

    print("Miss! Calling zenquotes API.")
    rep = http.get(DAILY_QUOTE_URL)
    if rep.status_code != 200:
        fail("Zenquotes request failed with status %d", rep.status_code)
    quote = rep.json()[0]["q"]
    author = rep.json()[0]["a"]
    cache.set("quote", str(quote), ttl_seconds=21600)
    cache.set("author", str(author), ttl_seconds=21600)

Hope this helps!

@kaushal - worked like a charm. Thanks so much!