83

My code creates a dictionary, which is then stored in a variable. I want to write each dictionary to a JSON file, but I want each dictionary to be on a new line.

My dictionary:

hostDict = {"key1": "val1", "key2": "val2", "key3": {"sub_key1": "sub_val2", "sub_key2": "sub_val2", "sub_key3": "sub_val3"}, "key4": "val4"}

Part of my code:

g = open('data.txt', 'a')
with g as outfile:
  json.dump(hostDict, outfile)

This appends each dictionary to 'data.txt' but it does so inline. I want each dictionary entry to be on new line. Any advice would be appreciated.

agf
  • 171,228
  • 44
  • 289
  • 238
Victor S
  • 839
  • 1
  • 6
  • 3

2 Answers2

158

Your question is a little unclear. If you're generating hostDict in a loop:

with open('data.txt', 'a') as outfile:
    for hostDict in ....:
        json.dump(hostDict, outfile)
        outfile.write('\n')

If you mean you want each variable within hostDict to be on a new line:

with open('data.txt', 'a') as outfile:
    json.dump(hostDict, outfile, indent=2)

When the indent keyword argument is set it automatically adds newlines.

agf
  • 171,228
  • 44
  • 289
  • 238
  • This did not seem to work for me; the problem disappeared after I stopped using `json.dump` or `json.dumps` on empty/nonexistent objects. – Ansa211 Oct 04 '19 at 11:16
  • 'json.dump(hostDict, outfile, indent=2)' this worked for me. thanks – Vineesh TP May 11 '20 at 11:19
  • 3
    Since you are working with JSON you may want to add a comma separator before the newline character: `outfile.write(',\n')` – datalifenyc Aug 19 '21 at 15:41
17

To avoid confusion, paraphrasing both question and answer. I am assuming that user who posted this question wanted to save dictionary type object in JSON file format but when the user used json.dump, this method dumped all its content in one line. Instead, he wanted to record each dictionary entry on a new line. To achieve this use:

with g as outfile:
  json.dump(hostDict, outfile,indent=2)

Using indent = 2 helped me to dump each dictionary entry on a new line. Thank you @agf. Rewriting this answer to avoid confusion.

Sayali Sonawane
  • 12,289
  • 5
  • 46
  • 47
  • 2
    This is pretty clearly not what the user is asking about, he's asking about storing multiple dicts in a [JSONL format](https://jsonlines.org/). But it's probably helpful for other people coming to this question. – Boris Verkhovskiy Jun 12 '21 at 10:40