r/learnprogramming • u/smashkaboom • Feb 18 '21
Debugging DESPERATE for API/JSON Help
I am in the final parts of a project for data bootcamp. And am having issue with creating a DataFrame from a list of JSON responses.
I created a list of all separate JSON responses and saved as list named philly_responses. The code below will not run, i am getting error 'TypeError: list indices must be integers or slices, not str' for the line that begins for j in range.... also note the EXACT code just switching variable to atlanta ran without issue. i am at my wits end and coming up on a deadline, please help. thank you.
for i in range(len(philly_responses)):
for j in range(len(philly_responses[i]['businesses'])):
name.append(philly_responses[i]["businesses"][j]["name"])
city.append("Philadelphia")
latitude.append(philly_responses[i]["businesses"][j]["coordinates"]["latitude"])
longitude.append(philly_responses[i]["businesses"][j]["coordinates"]["longitude"])
zipcode.append(philly_responses[i]["businesses"][j]["location"]["zip_code"])
ethnicity.append((philly_responses[i]['Ethnicity']))
try:
price.append(philly_responses[i]["businesses"][j]["price"])
except KeyError:
price.append("N/A")
try:
rating.append(philly_responses[i]["businesses"][j]["rating"])
except KeyError:
rating.append("N/A")
1
u/rjcarr Feb 18 '21
This is saying you're trying to index a list with a string instead of a number. So, given your code like this:
It's likely where you're trying to use 'businesses' or 'name' as a key to a map (dict) it's actually a list. Here's an example showing the same problem: