0

I am trying to use the NBA API and I have a big problem with returning invalid JSON strings.

I am using a code

from nba_api.stats.static import players
from nba_api.stats.endpoints import shotchartdetail

player_dictionary = players.get_players()

for player in player_dictionary:

    if player['is_active'] == True:
        print(player['last_name'])
        shotlog = shotchartdetail.ShotChartDetail(
        team_id = 0,
        player_id = player['id'],
        context_measure_simple = 'FGA',
        season_type_all_star = ['Regular Season', 'Playoffs'])

        shotlog.get_data_frames()[0].to_csv(f'NBA_data.csv', mode='a', index=False, header=False)

        time.sleep(.600)

The code works fine for the first 30-40 players in a player_dictionary and then crashes and gives an error ending with:

    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

From what I understand, it means that the returned JSON string is empty. However, it always crashes in different place in the players_dictionary. Does anyone know what is wrong? Is there any way to skip the iteration of the loop whenever JSON is invalid?

0 Answers0