I’m working with an API that returns JSON data and I need to access some nested information. Here’s what I’m trying to do:
api_response = requests.get("https://api.example.com/data/feed")
data = json.loads(api_response.text)
print data["items"]["metadata"]["url"]
The data variable contains a dictionary after I parse the JSON response. I’m attempting to navigate through the nested structure to get a specific URL value, but I keep running into this error:
Traceback (most recent call last):
File "api_parser.py", line 8, in <module>
print data["items"]["metadata"]["url"]
TypeError: list indices must be integers, not str
What’s the right way to handle this situation? I think the issue might be that one of the nested elements is actually a list instead of a dictionary, but I’m not sure how to fix my code.