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.”}