Python JSON json.dump() in Python














































Python JSON json.dump() in Python



json.dump() in Python

json module in Python module provides a method called dump() which converts the Python objects into appropriate json objects. It is a slight variant of dumps() method.

dump() and its arguments

Syntax: json.dump(d, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None)

import json 

# python object(dictionary) to be dumped 
dict1 ={ 
	"emp1": { 
		"name": "Lisa", 
		"designation": "programmer", 
		"age": "34", 
		"salary": "54000"
	}, 
	"emp2": { 
		"name": "Elis", 
		"designation": "Trainee", 
		"age": "24", 
		"salary": "40000"
	}, 
} 

# the json file where the output must be stored 
out_file = open("myfile.json", "w") 

json.dump(dict1, out_file, indent = 6) 

out_file.close() 


output:





Comments