0

Okay, I have been running around in circles. I have a list of dictionaries I am trying to serialize to a JSON file. I am using:

with open('/file_path/test.json', 'w') as write_file:
    json.dump(rdf_dic, write_file, separators = (',\n', ':'))

This works fine but my output looks like:

[  {"field_1":"1",
    "error_id":2,
    "another_feature":"yes"},
   {"field_1":"1",
    "error_id":2,
    "another_feature":"yes"},
   {"field_1":"no",
    "error_id":2,
    "another_feature":"ugh"}
]

etc. etc. for ~27k records.

However, I need each dictionary to exist on one line only. I know it sounds bizarre, but this file is being submitted and those are the requirements. I have searched high and low and truly am at a loss.

As a bonus, I also need the commas separating each record to not be there.

I also try:

with open('/file_path/test.json', 'w') as write_file:
    for record in rdf_dic:
        json.dump(record, write_file, separators = (',\n', ':'))

and receive almost identical results (just without the dictionaries being wrapped in a list).

ANY help would be greatly appreciated.

Forest
  • 23
  • 2
  • 1
    Have you tried _not_ specifying any `separators`? – ForceBru Oct 06 '20 at 20:42
  • 1
    What you are looking for is called "JSON Lines". Refer to this question: [Python conversion from JSON to JSONL](https://stackoverflow.com/questions/38915183/python-conversion-from-json-to-jsonl) – Jab Oct 06 '20 at 20:43
  • @Jab thank you SO much. Seriously. – Forest Oct 06 '20 at 20:56

0 Answers0