Python json.dumps() outputs all my data into one line but I want to have a new line for each entry

Viewed 10351

I am working with Python and some json data. I am looping through my data (which are all dictionaries) and when I print the loop values to my console, I get 1 dictionary per line.

However, when I do the same line of code with json.dumps() to convert my object into a string to be able to be output, I get multiple lines within the dictionary versus wanting the new line outside the dictionary.

How do you add new lines after each dictionary value when looping?

Code example:

    def test(values, filename):
      with open(filename, 'w') as f:
        for value in values:
            print(json.dumps(value, sort_keys=True)) # gives me each dictionary in a new line
            f.write(json.dumps(value, sort_keys=True, indent=0) #gives me a new line for each key/value pair instead of after each dictionary. 

Output in the console:

{"first_name": "John", "last_name": "Smith", "food": "corn"}
{"first_name": "Jane", "last_name": "Doe", "food": "soup"}

Output in my output file:

{"first_name": "John", "last_name": "Smith", "food": "corn"}{"first_name": "Jane", "last_name": "Doe", "food": "soup"}

What code am I missing to get a new line for each dictionary value so that my output file looks the same as my console?

3 Answers

If you want to produce valid JSON file you need to write all values at once, not one value at a time (which will produce ndjson or JSON lines)

So, for a valid JSON

values = [{"first_name": "John", "last_name": "Smith", "food": "corn"},
          {"first_name": "Jane", "last_name": "Doe", "food": "soup"}]

import json
with open('some_file.json', 'w') as f:
    json.dump(values, f, indent=4)

some_file.json

[
    {
        "first_name": "John",
        "last_name": "Smith",
        "food": "corn"
    },
    {
        "first_name": "Jane",
        "last_name": "Doe",
        "food": "soup"
    }
]

if you really need ndjson - you can use ndjson package (need install via pip from PyPi).

import ndjson

with open('some_file2.ndjson', 'w') as f:
    ndjson.dump(values, f)

some_file2.ndjson

{"first_name": "John", "last_name": "Smith", "food": "corn"}
{"first_name": "Jane", "last_name": "Doe", "food": "soup"}

Alternative to ndjson package is jsonlines package

You can newline after each f.write(json.dumps(value, sort_keys=True, indent=0 like this - f.write('\n')

You just have to append a newline character after every iteration.

f.write("\n")

Also please don't be confused with writelines method. It doesn't append newline character either, so you have to take care of it yourself anyway.

Related