A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/json-dump-in-python/ below:

json.dump() in Python - GeeksforGeeks

json.dump() in Python

Last Updated : 03 Jul, 2025

json.dump() method in Python is used to serialize a Python object into a JSON formatted string and write it directly into a file. This method is part of the built-in json module, which is useful when you need to save Python data structures like dictionaries or lists in JSON format.

Syntax

json.dump(obj, file, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None)

Parameters:

Let's look at some examples:

Example 1: Writing JSON Data to a File with Indentation

In this example, we will demonstrate how to write Python data to a JSON file while formatting it with indentation for better readability.

Python
import json

data = {
    "emp1": {"name": "Lisa", "age": 34, "salary": 54000},
    "emp2": {"name": "Elis", "age": 24, "salary": 40000},
}

# Writing to a JSON file with indentation
with open("output.json", "w") as outfile:
    json.dump(data, outfile, indent=4)

Output:

json.dump() Example 2: Using skipkeys to Ignore Non-Serializable Keys

This example demonstrates how to use the skipkeys parameter to prevent errors when attempting to serialize non-serializable keys, like tuples.

Python
import json

data = {("address", "street"): "Brigade Road"}  # Tuple as key

# Writing to a JSON file with skipkeys=True
with open("output.json", "w") as outfile:
    json.dump(data, outfile, skipkeys=True)

Output:

{}

In the above example, the tuple ("address", "street") is ignored because it's not serializable, and no error is raised due to skipkeys=True.

Example 3: Writing Non-ASCII Characters

Here, we will demonstrate how to handle non-ASCII characters in the JSON output by setting ensure_ascii=False.

Python
import json

data = {"greeting": "¡Hola Mundo!"}

# Writing to a JSON file with ensure_ascii=False to preserve non-ASCII characters
with open("output.json", "w", encoding="utf8") as outfile:
    json.dump(data, outfile, ensure_ascii=False)

Output:

{

"greeting": "¡Hola Mundo!"

}

With ensure_ascii=False, the special characters are written as-is without being escaped.

Example 4: Writing JSON with NaN and Infinity Values

This example shows how to handle special floating-point values like NaN and Infinity by adjusting the allow_nan parameter.

Python
import json

data = {"value": float("nan"), "infinity": float("inf")}

# Writing to a JSON file with allow_nan=True
with open("output.json", "w") as outfile:
    json.dump(data, outfile, allow_nan=True)

Output:

{

"value": NaN,

"infinity": Infinity

}

By setting allow_nan=True, we can serialize special float values like NaN and Infinity.

Difference between dump() and dumps() dump() dumps() The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument. The dumps() does not require any such file name to be passed. This method writes in the memory and then command for writing to disk is executed separately This method directly writes to the json file Faster method 2 times slower

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4