Correct way to write list of json to file in Python

Viewed 308

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?

2 Answers

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"}

You are looking for the * decorator for lists on print:

>>> api_responses = [{'id':'1', 'first_name':'John', 'last_name':'Smith'}, 
                     {'id':'2', 'first_name':'Chris', 'last_name':'Clark'}]
>>> print(*api_responses,sep="\n", file=open('filename.txt','w'))
    {'id': '1', 'first_name': 'John', 'last_name': 'Smith'}
    {'id': '2', 'first_name': 'Chris', 'last_name': 'Clark'}

Note that this answer solves perfectly what OP is asking. Open the file filename.txt and see that matches with op requests.

Note: request is to use simple quote ', and this answer match the requierement.

Related