Last updated: Apr 9, 2024
Reading timeยท2 min
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.
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}'
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.
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.
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.
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 indent
keyword argument pretty prints the JSON string with the specified
indent level. The argument is set to None
by default.
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.
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
Similarly, if you need to add a space only between the key-value pairs, set the separator to a comma and a space.
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
You can learn more about the related topics by checking out the following tutorials: