dumps(obj,indent=None, separators=None,skipkeys=False,sort_keys=False,
ensure_ascii=True, check_circular=True, allow_nan=True,
cls=None, encoding="utf-8", default=None )
obj : Required, The input python objecct. The list of objects can be used is given below import json
my_list=['Alex','Ronald','John']
print(my_list) # ['Alex', 'Ronald', 'John']
j=json.dumps(my_list)
print(j) # ["Alex", "Ronald", "John"]
List with Boolean value and None type
import json
my_list=[True, False, True,None]
print(my_list) # [True, False, True, None]
j=json.dumps(my_list)
print(j) # [true, false, true, null]
Using a tuple
import json
my_tuple=('Alex','Ronald','John')
print(my_tuple) # ('Alex', 'Ronald', 'John')
j=json.dumps(my_tuple)
print(j) # ["Alex", "Ronald", "John"]
Using Dictionary
import json
my_dict={1:'Alex',2:'Ronald'}
print(my_dict) # {1: 'Alex', 2: 'Ronald'}
j=json.dumps(my_dict)
print(j) # {"1": "Alex", "2": "Ronald"}
We can't use set , it will generate TypeError
import json
print(json.dumps(22)) # 22
print(json.dumps(22.34)) # 22.34
print(json.dumps(True)) # true
print(json.dumps(False)) # false
print(json.dumps(None)) # null
Using optional parameters separators and sort_keys
import json
my_dict={3:'Ronn',1:'Alex',2:'Ronald'}
print(my_dict) # {3: 'Ronn', 1: 'Alex', 2: 'Ronald'}
j=json.dumps(my_dict,separators=(';',':'),sort_keys=True)
print(j) # {"1":"Alex";"2":"Ronald";"3":"Ronn"}
Using indent
import json
my_dict={3:'Ronn',1:'Alex',2:'Ronald'}
print(my_dict) # {3: 'Ronn', 1: 'Alex', 2: 'Ronald'}
j=json.dumps(my_dict,indent=3)
print(j)
Output
{3: 'Ronn', 1: 'Alex', 2: 'Ronald'}
{
"3": "Ronn",
"1": "Alex",
"2": "Ronald"
}
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.