Reputation: 113
When I open and write a valid json into a file with the command below, it writes newline and carriage return characters into the data.
with open('logs/output.json', 'w') as outfile:
json.dump(json_data, outfile, indent=2, separators=(',', ': '))
output.json looks something like this:
{\r\n \"config\": {\r\n \"app\": {\r\n \"calendar\": {\r\n \"{tenant-key}calendar.reference }
How can I prevent this?
Upvotes: 9
Views: 38042
Reputation: 43
I got something similar to work by doing something like this:
newJson = json.dump(json_data, outfile, indent=2, separators=(',', ': '))
with open('logs/output.json', 'w') as json_file:
json_file.write(newJson)
Upvotes: 2
Reputation: 9441
json.dump(json_data, outfile, separators=(',', ':'))
Indent keywords argument needed only if you want indents on your new lines. You're activating "pretty print" by doing that.
Upvotes: 5