Json Dump in Python Writing Newline Character and Carriage Returns in File.

How to explicitly set carriage return when doing json.dump?

If you insist on consistent CRLF behavior (the JSON spec requires parsers to handle both, but opening it in certain plain text readers like Notepad might be easier with consistent CRLF), the solution is in the open function, not the json module.

Just pass newline='\r\n' to open, and it will translate any \n written by json to \r\n seamlessly on all systems, rather than the default behavior of translating to os.linesep (which is \r\n on Windows and \n on most other OSes):

with open(fpath, 'w', encoding="utf-8", newline='\r\n') as outfile:
json.dump(data, outfile, indent=4, sort_keys=True, ensure_ascii=False)

Removing a new line feed in json file using Python.

Reading between the lines, I think the input format might be a single JSON array, and the desired output is newline-separated JSON representations of the elements of that array. If so, this is probably all that's needed:

with open('testnoline.json', 'w') as outfile:
for obj in data_json:
outfile.write(json.dumps(obj) + "\n")

Python: Handling newlines in json.load() vs json.loads()

json.load() reads from a file descriptor and json.loads() reads from a string.

Within your file, the \n is properly encoded as a newline character and does not appear in the string as two characters, but as the correct blank character you know.

But within a string, if you don't double escape the \\n then the loader thinks it is a control character. But newline is not a control sequence for JSON (newline is in fact a character like any other).

By doubling the backslash you actually get a real string with \n in it, and only then will Python transform the \n into a newline char.

How to add a newline function to JSON using Python

If you wish to write your data exactly as you have read it in, then you will need to iterate over each dictionary in dicts:

with open('firetobq_peripheral5.json', 'w') as out_file:
for d in dicts:

If this is not required, then json.dump would be the best option.

Python3 remove newline character from json response

You try to call .replace on a dictionary. You should call .replace on the string:

content = json.loads(r.decode('utf-8').replace('\n', ''))

However, keep in mind that this may invalidate the string as json, depending on its content.

BTW, if you use requests you can get json directly:

import requests

content = requests.get("http://example.com/file.json").json()

How to end a JSON file with "\r\n"?

\r is a means of expressing the carriage return character -- a control signal which sends the cursor back to the beginning of the line. \n is a linefeed -- a signal which sends the cursor down to the next line.

On DOS and Windows, \r\n (aka CRLF) is the traditional way to end a line of text. Presumably, then, this means that your software is expected to write a JSON document on one line, followed by a DOS/Windows-style newline.

socket.write(json.dumps({'test': 'EXAMPLE'}, sort_keys=True) + '\r\n')

Note that indent=4 is not specified here, because setting any value other than None for indent means that you want your data split onto multiple (indented) lines -- whereas if your format ends in \r\n, that implies that the data is all expected to be on a single line.

Python write valid json with newlines to file

You ran into the pitfall of neglecting the fact that the \ character is also an escape sequence character in Python. Try printing out the last example instead of calling json.loads:

>>> print('{"mystring": "Line 1\nLine 2"}')
{"mystring": "Line 1
Line 2"}

No way the above is valid JSON. What if the \ character is correctly encoded?

>>> print('{"mystring": "Line 1\\nLine 2"}')
{"mystring": "Line 1\nLine 2"}

Much better, you can then:

>>> json.loads('{"mystring": "Line 1\\nLine 2"}')
{'mystring': 'Line 1\nLine 2'}

Alternatively, if you really appreciate being able to copy some text from some other buffer and paste it into your live interpreter to do decode, you may consider using the raw modifier for your string:

>>> print(r'{"mystring": "Line 1\nLine 2"}')
{"mystring": "Line 1\nLine 2"}
>>> json.loads(r'{"mystring": "Line 1\nLine 2"}')
{'mystring': 'Line 1\nLine 2'}

See that the \ is no longer automatically escaping with the newline.

Also see: How do I handle newlines in JSON? and note how this is not a problem that exists strictly within Python.

Related Topics

Leave a reply