Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create newline delimited json-lines file from list of python dictionaries

I am trying to create a JSON-lines file of data so that is compatible with google cloud AI platform's requirements for online prediction.

Right now I have a list of dictionaries for each of my data points. It looks like this:

data = [{'values': [0,1,0], 'key': 0}, {'values': [1,1,0], 'key': 1}]

I'm exporting this data to data.json with the following code:

import json
json_filepath = "data.json"
with open(json_filepath, 'w') as f:
    json.dump(data, f)

The problem is, this data.json file then looks exactly like my data (viz. a list of dictionaries). How can I make this data.json file a new-line delimited collection of each dictionary in the list? In other words, how can I make it look like this:

{'values': [0,1,0], 'key': 0}
{'values': [1,1,0], 'key': 1}
like image 965
TrentWoodbury Avatar asked Dec 06 '22 09:12

TrentWoodbury


1 Answers

You can loop through the array and dump each object followed by a new line '\n':

with open(json_filepath, 'w') as f:
    for d in data:
        json.dump(d, f)
        f.write('\n')

Alternatively, you can use a one-liner using json.dumps, str.join, and map:

with open(json_filepath, 'w') as f:
    f.write('\n'.join(map(json.dumps, data)))
like image 123
MrGeek Avatar answered Dec 25 '22 20:12

MrGeek