Skip to main content

Create compact JSON with Python

Calling json.dumps(…, separators=(',', ':')) will reduce the amount of whitespace in the final string.

To create compact JSON in Python:

>>> json.dumps({'a': 1, 'b': 2})
'{"a": 1, "b": 2}'

>>> json.dumps({'a': 1, 'b': 2}, separators=(',', ':'))
'{"a":1,"b":2}'

(via Raymond Hettinger)