Python - json without spaces

I only realized that json.dumps() adds spaces in the JSON object

eg.

 {'duration': '02:55', 'name': 'flower', 'chg': 0} 

How can I remove spaces to make JSON more compact and save bytes for sending over HTTP?

eg:

 {'duration':'02:55','name':'flower','chg':0} 
+81
json python
May 01 '13 at 1:42
source share
2 answers
 json.dumps(separators=(',', ':')) 
+131
May 01 '13 at 1:45
source share

In some cases, you can only get rid of trailing spaces . Then you can use

 json.dumps(separators=(',', ': ')) 

There is a space after : but not after,.

This is useful for delimiting your JSON files (in version control, such as git diff ), where some editors get rid of the trailing space, but python json.dump will add it back.

Note. This certainly does not answer the question from above, but I came here in search of this answer. I don't think it deserves its own QA, so I add it here.

+19
Jan 26 '16 at 12:14
source share



All Articles