Using json.dumps() without spaces in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Using json.dumps() without spaces in Python #

Use the separators keyword argument to use the json.dumps() method without spaces, e.g. json_str = json.dumps(employee, separators=(',', ':')). The separators argument can be set to a tuple containing a comma and a colon to remove the whitespace.

main.py
import json employee = { 'name': 'Alice', 'age': 30, 'salary': 100, } # 👉️ default separators are (', ', ': ') # ✅ json string without spaces json_str = json.dumps(employee, separators=(',', ':')) print(json_str) # 👉️ '{"name":"Alice","age":30,"salary":100}' # ----------------------------------------------- # ✅ json string with spaces only between keys and values json_str = json.dumps(employee, separators=(',', ': ')) print(json_str) # ✅ '{"name": "Alice","age": 30,"salary": 100}' # ----------------------------------------------- # ✅ json string with spaces only between key-value pairs json_str = json.dumps(employee, separators=(', ', ':')) print(json_str) # ✅ '{"name":"Alice", "age":30, "salary":100}'

We used the separator keyword argument to add no extra whitespace to the output of the json.dumps() method.

The json.dumps method converts a Python object to a JSON formatted string.

The separators argument is a tuple containing 2 values - the separator between each key-value pair and the separator between each key and value.

The default value for the argument is (', ', ': ') if the indent argument is None and (',', ': ') if indent is set to any other value.

main.py
import json employee = { 'name': 'Alice', 'age': 30, 'salary': 100, } # 👇️ default behavior print(json.dumps(employee)) # 👇️ {"name": "Alice", "age": 30, "salary": 100} print(len(json.dumps(employee))) # 👉️ 43 # 👇️ with whitespace removed json_str = json.dumps(employee, separators=(',', ':')) print(json_str) # 👉️ '{"name":"Alice","age":30,"salary":100}' print(len(json_str)) # 👉️ 38

If you still get whitespace when converting to JSON, make sure you aren't passing a value for the indent argument.

main.py
import json employee = { 'name': 'Alice', 'age': 30, 'salary': 100, } # { # "name":"Alice", # "age":30, # "salary":100 # } json_str = json.dumps(employee, indent=4, separators=(',', ':')) print(len(json_str)) # 👉️ 54

The indent keyword argument pretty prints the JSON string with the specified indent level. The argument is set to None by default.

To remove all whitespace from the JSON string, either set the indent argument to None or omit it when calling json.dumps().

If you need to add whitespace only between the keys and the values, set the separator to a colon and a space.

main.py
import json employee = { 'name': 'Alice', 'age': 30, 'salary': 100, } json_str = json.dumps(employee, separators=(',', ': ')) print(json_str) # 👉️ '{"name": "Alice","age": 30,"salary": 100}' print(len(json_str)) # 👉️ 41

Similarly, if you need to add a space only between the key-value pairs, set the separator to a comma and a space.

main.py
import json employee = { 'name': 'Alice', 'age': 30, 'salary': 100, } json_str = json.dumps(employee, separators=(', ', ':')) print(json_str) # 👉️ '{"name":"Alice", "age":30, "salary":100}' print(len(json_str)) # 👉️ 40
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.