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!
It's not much work: you can specify the newlines you wish to use when you open the output file:
This should also work if you're writing back to the source file: