0

I'm struggling with kind of a trivial issue. I call an API endpoint and receive the following response as an object:

{
    'data': [{
                'id': 18136910,
                'league_id': 501,
                'season_id': 18369,
                'stage_id': 77453684,
                'round_id': 247435,
                'group_id': None,
                'aggregate_id': None,
                'venue_id': 219,
             }  
             { ....
  1. Approach: Turn object into dict and grab data
print(response.json().data)

# Prints AttributeError: 'dict' object has no attribute 'data'
  1. Approach to enter the object
print(response.data) or print(response.data[0])

# Prints AttributeError: 'Response' object has no attribute 'data'

So how to assign e.g. 'id' to a variable x?

0

2 Answers 2

1

You are using the dict incorrectly. To get the id try:

print(response.json()['data'][0]['id'])

To access the value with key "x" of a dict d you should use d["x"] or d.get("x").

Sign up to request clarification or add additional context in comments.

2 Comments

how would I iterate over data? for item in data: ?
Yes, for item in response.json()['data']: print(item['id'])
1

Try like this:

You need to access dict object with key.

res = response.json()
print(res['data'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.