JSON, which stands for JavaScript Object Notation, is a widely-used format for sending, receiving, and storing data. In Python, the json
 module is dedicated to handling JSON data. JSON data consists of key-value pairs enclosed in curly braces or square brackets, similar to Python dictionaries.
The json.dump
 function in Python is used to serialize Python objects into a JSON formatted stream.
Syntax
json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameters and Functionality
- obj: The Python object to be serialized into JSON.
- skipkeys: IfÂ
True
, dictionary keys that are not of a basic type will be skipped. Default isÂFalse
. - ensure_ascii: IfÂ
True
 (default), non-ASCII characters are escaped in the output. IfÂFalse
, non-ASCII characters will be output as-is. - check_circular: IfÂ
False
, the check for circular references in container types is skipped. Default isÂTrue
. - allow_nan: IfÂ
False
, it raises aÂValueError
 when serializing out-of-range float values (nan
,Âinf
,Â-inf
). Default isÂTrue
, which uses JavaScript equivalents (NaN
,ÂInfinity
,Â-Infinity
). - indent: If a non-negative integer or string, JSON array elements and object members will be pretty-printed with that indent level.
- separators: Specifies item and key separators. Default isÂ
(", ", ": ")
. - default: A function called for objects that can’t be serialized. It should return a JSON-encodable version of the object or raise aÂ
TypeError
. If not specified,ÂTypeError
 is raised. - sort_keys: IfÂ
True
, the output of dictionaries will be sorted by key. Default isÂFalse
.
Examples
Example 1: Basic Usage
Passing a Python dictionary to json.dumps
 returns a JSON string.
import json
# Creating a dictionary
d = {1: 'Board', 2: 'Infinity', 3: 'Forever'}
json_str = json.dumps(d)
print('Result of passing through json.dumps:', json_str)
print()
# Checking type of object
print(type(json_str))
Output:
Result of passing through json.dumps: {"1": "Board", "2": "Infinity", "3": "Forever"}
<class 'str'>
Example 2: Using skipkeys
Setting skipkeys
 to True
 skips non-basic type keys.
import json
# Creating a dictionary
d = {(45, 67, 86): 'Hello', 1: 'Board', 2: 'Infinity', 3: 'Forever'}
json_str = json.dumps(d, skipkeys=True)
print('Result of passing through json.dumps:', json_str)
print()
# Checking type of object
print(type(json_str))
Output:
Result of passing through json.dumps: {"1": "Board", "2": "Infinity", "3": "Forever"}
<class 'str'>
Example 3: Using allow_nan
Setting allow_nan
 to True
 allows the serialization of NaN
 values.
import json
# Creating a dictionary
d = {(45, 67, 86): 'Hello', 1: 'Board', 2: 'Infinity', 3: 'Forever', 4: float('nan')}
json_str = json.dumps(d, skipkeys=True, allow_nan=True)
print('Result of passing through json.dumps:', json_str)
print()
# Checking type of object
print(type(json_str))
Output:
Result of passing through json.dumps: {"1": "Board", "2": "Infinity", "3": "NaN"}
<class 'str'>
Additional Information
Formatting the Result
The json
 module provides built-in methods for formatting the result. The indent
 parameter determines the indentation level for pretty-printing.
json.dumps(y, indent=5)
You can also define custom separators to separate objects. By default, separators are (", ", ": ")
.
json.dumps(y, indent=5, separators=(".", " = "))
Ordering the Result
The result of json.dumps
 can be ordered by setting the sort_keys
 parameter to True
.
json.dumps(y, indent=5, sort_keys=True)
Conclusion
By the end of this section, you should understand what JSON is and how to convert Python objects to JSON format. Additionally, you will be familiar with the various parameters of the json.dump
 function and their effects on the output.