Using Python, I'm pulling json data from an API - every time I get a response, I add it to a list named api_responses like so:
api_responses = [{'id':'1', 'first_name':'John', 'last_name':'Smith'},{'id':'2', 'first_name':'Chris', 'last_name':'Clark'}]
I need to write the records to a file so that it's only the records and not a list:
api_responses.json:
{'id':'1', 'first_name':'John', 'last_name':'Smith'}
{'id':'2', 'first_name':'Chris', 'last_name':'Clark'}
I could for-loop the list, write each row and add '\n'.
with open('api_responses.json', 'w') as outfile:
for response in api_responses:
json.dump(response, outfile)
outfile.write('\n')
I don't want to indent or pretty the json - just make it flat as possible. Is there a better way to do this? Is jsonl/jsonlines what I am searching for?