Using json.dumps() without Spaces in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
2 min

banner

# Using json.dumps() without Spaces in Python

Use the separators keyword argument to use the json.dumps() method without spaces.

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': 'Bobby', 'age': 30, 'salary': 100, } # ๐Ÿ‘‰๏ธ default separators are (', ', ': ') # โœ… JSON string without spaces json_str = json.dumps(employee, separators=(',', ':')) print(json_str) # ๐Ÿ‘‰๏ธ '{"name":"Bobby","age":30,"salary":100}' # ----------------------------------------------- # โœ… JSON string with spaces only between keys and values json_str = json.dumps(employee, separators=(',', ': ')) print(json_str) # โœ… '{"name": "Bobby","age": 30,"salary": 100}' # ----------------------------------------------- # โœ… JSON string with spaces only between key-value pairs json_str = json.dumps(employee, separators=(', ', ':')) print(json_str) # โœ… '{"name":"Bobby", "age":30, "salary":100}'

using json dumps without spaces

The code for this article is available on GitHub

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': 'Bobby', 'age': 30, 'salary': 100, } # ๐Ÿ‘‡๏ธ default behavior print(json.dumps(employee)) # ๐Ÿ‘‡๏ธ {"name": "Bobby", "age": 30, "salary": 100} print(len(json.dumps(employee))) # ๐Ÿ‘‰๏ธ 43 # ๐Ÿ‘‡๏ธ with whitespace removed json_str = json.dumps(employee, separators=(',', ':')) print(json_str) # ๐Ÿ‘‰๏ธ '{"name":"Bobby","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': 'Bobby', 'age': 30, 'salary': 100, } # { # "name":"Bobby", # "age":30, # "salary":100 # } json_str = json.dumps(employee, indent=4, separators=(',', ':')) print(len(json_str)) # ๐Ÿ‘‰๏ธ 54
The code for this article is available on GitHub

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().

# Adding spaces only between the keys and the values

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': 'Bobby', 'age': 30, 'salary': 100, } json_str = json.dumps(employee, separators=(',', ': ')) print(json_str) # ๐Ÿ‘‰๏ธ '{"name": "Bobby","age": 30,"salary": 100}' print(len(json_str)) # ๐Ÿ‘‰๏ธ 41

adding spaces only between the keys and the values

The code for this article is available on GitHub

# Adding spaces only between the key-value pairs

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': 'Bobby', 'age': 30, 'salary': 100, } json_str = json.dumps(employee, separators=(', ', ':')) print(json_str) # ๐Ÿ‘‰๏ธ '{"name":"Bobby", "age":30, "salary":100}' print(len(json_str)) # ๐Ÿ‘‰๏ธ 40

adding spaces only between the key value pairs

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev