'How do i select a specific part of a api response in flask

Code:

@app.route("/")
def home():
    
    connection = http.client.HTTPConnection('api.football-data.org')
    headers = { 'X-Auth-Token': 'My api key' }
    connection.request('GET', 'https://api.football-data.org/v2/competitions/CL/matches?status=FINISHED', None, headers )
    response = json.loads(connection.getresponse().read().decode())

    return response

Response:

{
"competition": {
"area": {
"id": 2077,
"name": "Europe"
},
"code": "CL",
"id": 2001,
"lastUpdated": "2022-03-20T09:20:44Z",
"name": "UEFA Champions League",
"plan": "TIER_ONE"
},
"count": 216,
"filters": {
"status": [
"FINISHED"
]
},
"matches": [
{
"awayTeam": {
"id": 8912,
"name": "Inter Club d'Escaldes"
},
"group": null,
"homeTeam": {
"id": 8166,
"name": "HB Tórshavn"
},
"id": 328846,
"lastUpdated": "2022-05-04T16:20:11Z",
"matchday": null,
"referees": [],
"score": {
"duration": "REGULAR",
"fullTime": {
"awayTeam": 1,
"homeTeam": 0
},

"winner": "AWAY_TEAM"
}

I cut out some of the response so i could post the question

This is not the full response its just one part of it I want to get the awayTeam and the homeTeam name but i dont know, when i try

return response.matches[0].awayTeam.name

it gives me an error saying

AttributeError: 'dict' object has no attribute 'matches'



Solution 1:[1]

If response is a dictionary object, try viewing its keys with:

print(response.keys()) 

Then use those keys to access values within the dictionary

matches = response['matches']
example_match = response['matches'][0]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aloha_erich