How to prevent Python JSON .dumps() function from removing carriage return?

186 Views Asked by At

In Python, I'm attempting to read in a JSON file, change a single value, then do some further processing on the string returned from the .dumps() function. However, after calling json.dumps() the string that is returned has removed the /r characters.

It is important for my application the original formatting is maintained. The documentation for json.dumps() does not have a way to specify line endings. I can't see what else could be causing this removal.

Am I doing something wrong, or is there a way to maintain line endings without having to strong-arm the output (i.e. replace, writing to a new file with correct line endings, etc.)?

As a simple example:

with open('./test.json', 'rb') as test_file:
   for line in test_file:
      print(line)
      # This prints lines that look like:
      # b'{\r\n'
      # b'   "yay": "JSON!"\r\n'
      # b'}'
   test_file.seek(0,0)
   
   json_object = json.load(test_file)
   json_dump = json.dumps(json_object, indent=3)
   print(repr(json_dump))
   # This prints:
   # '{\n   "yay": "JSON!"\n}'
   #    ^\r removed       ^\r removed

EDIT

To clarify against this possible duplicate. I am hoping I can do this without modifying the existing file or writing to a new one. In other words, I want the string returned from .dumps() to maintain the same line-endings as the file. However, thanks to helpful comments below. I see this really is not the intention of the JSON package. The JSON package only conveys JSON data, making no promises about formatting. Sounds like to get what I want I'll need to either write to a new file or do some string formatting myself. Thanks all!

1

There are 1 best solutions below

3
Chris On

without having to strong-arm the output (i.e. replace, writing to a new file with correct line endings, etc.)?

It's not much work: you can specify the newlines you wish to use when you open the output file:

with open("test.json", "w", newline="\r\n") as f:
    f.write('{\n   "yay": "JSON!"\n}')

When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

This should also work if you're writing back to the source file:

with open("test.json", "r+", newline="\r\n") as f:
    my_dict = json.loads(f.read())

    f.seek(0)
    f.write(json.dumps(my_dict, indent=3))