json.dumps()

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
indent : (optional ) None (Default) , an indent level.
separators : ( optional ) ',' is default, we can use our own
sort_keys : ( optional ) False ( Default ) sorting by keys if set to True
ensure_ascii : (Optional ) True ( Default ) All non ASCII char are escaped check_circular:(Optional ) True ( Default ) Check for circular reference if False
allow_nan : (Optional ) True ( Default ) Error will generate for out of range float values if False

Converting List to Json
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

We will use other python objects
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"
}

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate 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.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer