Reputation: 201
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.
Upvotes: 0
Views: 87
Reputation: 6234
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'])
Upvotes: 1