I am retrieving results from an API and have to paginate to get all results. The response is two fields. One is the cursor which I use to paginate, the other is 50 results in an array.
I am successfully loading multiple responses, but am missing a "," between arrays after they are written to the file.
with open(writeFilePath,'a') as outfile:
while more_results:
parameters = urllib.parse.urlencode(parms)
#other code
if 'cursor' in response_body:
parms['cursor']=response_body['cursor']
else:
more_results = False
json.dump(response_body['transactions'],outfile)
Questions:
- What is the best way to add a comma between arrays/objects from separate responses?
- Should I be writing the comma-separated arrays or drilling down to write the objects in each array? (the latter seems better as I'm writing this)
- If yes to #2, how do I dump the contents of response_body['transactions'] rather than the whole array?
I may end up loading 3 yrs of data so instead of continually adding results to one variable and writing it all to the file at the end, I am writing to the file one page at a time.