I’m working with a JSON response from an API and running into trouble when trying to access nested properties. Here’s my current approach:
api_response = requests.get("https://example-streaming-api.com/live")
data = json.loads(api_response.text)
print(data["channels"]["meta"]["url"])
The data variable contains a dictionary created from the JSON response. When I attempt to navigate through the nested structure to retrieve a specific URL value, I keep getting this error:
Traceback (most recent call last):
File "stream_parser.py", line 8, in <module>
print(data["channels"]["meta"]["url"])
TypeError: list indices must be integers, not str
It seems like I’m trying to use string keys on what might be a list instead of a dictionary. What’s the proper way to handle this nested JSON structure and avoid this TypeError?
JSON debugging sucks, but the real issue is you’re writing brittle code that breaks every time the API changes.
Your “channels” key returns a list, so you need data["channels"][0]["meta"]["url"] to grab the first item. But tomorrow they’ll restructure everything or return empty arrays.
I wasted tons of time writing defensive parsing code for APIs. Then I switched to Latenode for integrations. You can build workflows that handle HTTP requests and parse nested JSON without any code.
The JSON parser nodes automatically handle lists vs objects. Just map the fields you want and it extracts them reliably. When the API structure changes, you update the mapping visually instead of debugging Python crashes.
You also get error handling and retry logic built in. No more try-catch blocks everywhere or checking if keys exist.
The traceback says it all - you’re hitting a list when you expected a dictionary. Always check your JSON structure first before trying to access nested stuff. I wasted hours on similar bugs before learning this lesson.
I just throw in some quick debug prints to see what I’m actually working with:
api_response = requests.get("https://example-streaming-api.com/live")
data = json.loads(api_response.text)
print(json.dumps(data, indent=2)[:500]) # First 500 chars, nicely formatted
This shows you exactly what’s there. If “channels” is a list, you’ll need data["channels"][0]["meta"]["url"] for the first channel.
For production, I wrap everything in try-except blocks since APIs are unpredictable. Empty lists, missing keys - handle it gracefully or your app will crash.
yeah, super common with apis. use .get() instead of brackets - much safer. something like data.get("channels", [{}])[0].get("meta", {}).get("url"). won’t crash when keys are missing or the api structure shifts on you.
Hit this exact same issue with a streaming API last month. You’re trying to use a list like a dictionary - happens when the JSON isn’t structured how you think it is. API responses change depending on what’s happening. Sometimes “channels” comes back as an empty list when nothing’s streaming, sometimes it’s full of channel objects. Best bet is to code defensively:
api_response = requests.get("https://example-streaming-api.com/live")
data = json.loads(api_response.text)
if isinstance(data.get("channels"), list) and len(data["channels"]) > 0:
url = data["channels"][0]["meta"]["url"]
else:
url = None # or handle the empty case
This checks if channels exists, is a list, and actually has stuff in it before you try accessing nested properties. Saved me tons of crashes when the API returned different structures during maintenance or when no streams were live.
Honestly though, manually parsing nested JSON is a pain. APIs change their structure all the time, and you end up writing tons of error handling code.
I switched to using Latenode for JSON processing. You can build a workflow that handles the API call and extracts nested data automatically - no more list vs dict headaches. The built-in JSON parsing nodes handle this extraction stuff easily.
The visual builder shows you exactly how your data moves and transforms. Way better than guessing data types and writing defensive code.