0

I want to fetch specific data form JSON . Right now I can fetch all of the data and convert it into JSON format. But I only want to fetch "home_team" and "away_team" of all the sets.

My code to fetch all of the data is:`

import json
import requests

reap = requests.get('https://app.sportdataapi.com/api/v1/soccer/matches?apikey=64572f90-88d6-11eb-8a43-7106c933e99d&season_id=496&date_from=2020-09-19&fbclid=IwAR2zkLfVHG1xyxCrKQdcvCqhYhyjp5vA2TtbAsXZ3R3pVFJKV3jnhgFdjG4')
data = reap.json()
print(data)

And my JSON data is:

{'query': {'apikey': '64572f90-88d6-11eb-8a43-7106c933e99d', 'season_id': '496', 'date_from': '2020-09-19', 'fbclid': 'IwAR2zkLfVHG1xyxCrKQdcvCqhYhyjp5vA2TtbAsXZ3R3pVFJKV3jnhgFdjG4'}, 'data': [{'match_id': 139415, 'status_code': 3, 'status': 'finished', 'match_start': '2020-09-19 13:30:00', 'match_start_iso': '2020-09-19T13:30:00+00:00', 'minute': None, 'league_id': 314, 'season_id': 496, 'stage': {'stage_id': 1, 'name': 'Regular Season'}, 'group': {'group_id': 222, 'group_name': 'Bundesliga'}, 'round': {'round_id': 18564, 'name': '1', 'is_current': None}, 'referee_id': 433, 'home_team': {'team_id': 3993, 'name': '1. FC Union Berlin', 'short_code': 'UNI', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2819.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'away_team': {'team_id': 4075, 'name': 'FC Augsburg', 'short_code': 'FCA', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2814.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'stats': {'home_score': 1, 'away_score': 3, 'ht_score': '0-1', 'ft_score': '1-3', 'et_score': None, 'ps_score': None}, 'venue': {'venue_id': 1870, 'name': 'An der alten Forsterei', 'capacity': 22012, 'city': 'Berlin', 'country_id': 48}}, {'match_id': 139451, 'status_code': 3, 'status': 'finished', 'match_start': '2020-09-19 13:30:00', 'match_start_iso': '2020-09-19T13:30:00+00:00', 'minute': None, 'league_id': 314, 'season_id': 496, 'stage': {'stage_id': 1, 'name': 'Regular Season'}, 'group': {'group_id': 222, 'group_name': 'Bundesliga'}, 'round': {'round_id': 18564, 'name': '1', 'is_current': None}, 'referee_id': 466, 'home_team': {'team_id': 4070, 'name': 'Werder Bremen', 'short_code': 'SVW', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2810.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'away_team': {'team_id': 4067, 'name': 'Hertha BSC', 'short_code': 'BSC', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2806.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'stats': {'home_score': 1, 'away_score': 4, 'ht_score': '0-2', 'ft_score': '1-4', 'et_score': None, 'ps_score': None}, 'venue': {'venue_id': 1930, 'name': 'Weserstadion', 'capacity': 42100, 'city': 'Bremen', 'country_id': 48}}, {'match_id': 139479, 'status_code': 3, 'status': 'finished', 'match_start': '2020-09-19 13:30:00', 'match_start_iso': '2020-09-19T13:30:00+00:00', 'minute': None, 'league_id': 314, 'season_id': 496, 'stage': {'stage_id': 1, 'name': 'Regular Season'}, 'group': {'group_id': 222, 'group_name': 'Bundesliga'}, 'round': {'round_id': 18564, 'name': '1', 'is_current': None}, 'referee_id': 39, 'home_team': {'team_id': 3991, 'name': '1. FC Cologne', 'short_code': 'KOE', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2809.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'away_team': {'team_id': 4079, 'name': 'TSG 1899 Hoffenheim', 'short_code': 'TSG', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2818.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 

Now if I do:

print(data['data'])

It also show me all of the data except query and insider data of query.

6
  • You just have to iterate over the data, and drill down to the home_team and away_team keys and grab their values. Commented Mar 25, 2021 at 13:41
  • I already know what you are saying but i do not how to do this. Commented Mar 25, 2021 at 13:43
  • Please make sure your example data is both manageable (not thousands of characters long) and correct (it currently is open-ended). That makes it easier for people to try your data, and provide an answer (possibly with an example). Commented Mar 25, 2021 at 13:44
  • Just start at the beginning, and try, bit by bit. Commented Mar 25, 2021 at 13:44
  • Sir the data is too long . I can not post all the data here. This is for just how to make the loop for this and grab home and away team from it Commented Mar 25, 2021 at 13:46

1 Answer 1

1

data key in your response JSON is a list of dict, you can iterate over the list using a for loop and access away_team and home_team dictionaries.

for d in data['data']:
    print(d['home_team'])
    
for d in data['data']:
    print(d['away_team'])
Sign up to request clarification or add additional context in comments.

1 Comment

TypeError: string indices must be integers

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.