Reputation: 2613
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,
}
{ ....
print(response.json().data)
# Prints AttributeError: 'dict' object has no attribute 'data'
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
?
Upvotes: 0
Views: 429
Reputation: 764
res = response.json()
print(res['data'])
Upvotes: 1
Reputation: 833
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")
.
Upvotes: 1