Correct way to write list of json to file in Python

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?

Answer

Your code already correctly outputs the JSON objects as individual lines into a file, also known as JSONL as you pointed out.

To make the code slightly more concise and readable, you can take advantage of the print function, which outputs a newline character for you by default:

with open('api_responses.json', 'w') as outfile:
    for response in api_responses:
        print(json.dumps(response), file=outfile)

With your sample input, this outputs:

{"id": "1", "first_name": "John", "last_name": "Smith"}
{"id": "2", "first_name": "Chris", "last_name": "Clark"}