How to send a json object pretty printed as an email python
I can't say for certain, but it would seem like your email-sending code is defaulting to sending an "HTML" email, and in HTML consecutive spaces collapse into one, that way HTML code like:
<p> This is a paragraph, but it's long so I'll break to a new line, and indented so I know it's within the `p` tag, etc.</p>
Looks like "This is a paragraph, but it's long so I'll break to a new line, and indented so I know it's within the p
tag, etc." to the user.
So, I'd say your two options are:
- Change the email sending code to send the
Content-type
header astext/plain
, or Replace all your spaces with the
<br>
(breaks), for example:email_body = json.dumps( health, indent=4, sort_keys=True).replace(' ', ' ').replace('\n', '<br>')
>>> import json>>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)>>> print s{ "4": 5, "6": 7}
mine works finePython 2.7.3 (default, Jan 17 2015, 17:10:37) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
maybe it's version related, post your version and output