I am pulling data in from an API that limits the number of records per request to 100. There are 7274 records in total and everything is returned as JSON.
I want to concatenate all 7274 records into a single variable/object and eventually export to a JSON file.
The response JSON objects are structured like this:
{"data":[{"key1":87,"key2":"Ottawa",..."key21":"ReggieWatts"}],"total":7274}
I just want the objects inside the "data" array so that the output looks like this:
{'key1': 87, 'key2': 'Ottawa', 'key21': 'ReggieWatts'},{'key1': 23, 'key2': 'Cincinnati', 'key21': 'BabeRuth'},...
I’ve tried without success to use the dict.update() method to concatenate the new values to a variable that’s collecting all the records.
I am getting this error:
ValueError: dictionary update sequence element #0 has length 21; 2 is required
Here’s the stripped down code.
import json
import time
import random
import requests
from requests.exceptions import HTTPError
api_request_limit = 100
# total_num_players = 7274
total_num_players = 201 # only using 201 for now so that we aren't hammering the api while testing
start_index = 0
base_api_url = "https://api.nhle.com/stats/rest/en/skater/bios?isAggregate=true&isGame=false&sort=[{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&limit=100&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20202021%20and%20seasonId%3E=19171918&start="
player_data = {}
curr_data = {}
for curr_start_index in range(start_index, total_num_players, api_request_limit):
api_url = base_api_url + str(curr_start_index)
try:
response = requests.get(api_url)
# if successful, no exceptions
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
else:
# print('Success!')
curr_data = response.json()
player_data.update(curr_data['data'])
# player_data = {**player_data, **curr_data['data']} # Does not work either
# print(player_data)
# for i in curr_skaters['data']:
# print(str(i['firstSeasonForGameType']) + ": " + str(i['skaterFullName']) + " " + str(i['playerId']))
set_delay = (random.random() * 2) + 1
time.sleep(set_delay)
Should I be iterating through each of the 100 records individually to add them to player_data?
The ValueError
implies that the issue is with the number of key:value pairs in each object which says to me I'm using the .update()
method incorrectly here.
Thanks