Fix Python – Python – json without whitespaces

Question

Asked By – Daniele B

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

e.g.

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

how can remove the spaces in order to make the JSON more compact and save bytes to be sent via HTTP?

such as:

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

Now we will see solution for issue: Python – json without whitespaces


Answer

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

This question is answered By – donghyun208

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0