Of course! In Python, the json library is the standard way to work with JSON data. The indent parameter is a key feature for making JSON human-readable.

Here's a complete guide, from the basics to more advanced usage.
The Basics: json.dumps() with indent
The primary function for converting a Python object to a JSON string is json.dumps() (dumps = dump string). The indent parameter controls the pretty-printing.
How it Works:
- If you don't use
indent, the output is a compact, single-line string. - If you use
indent=4(or any positive integer), the output is formatted with newlines and indentation for readability.
Example:
Let's start with a Python dictionary.
import json
data = {
"name": "John Doe",
"age": 30,
"isStudent": False,
"courses": [
{"title": "History", "credits": 3},
{"title": "Math", "credits": 4}
],
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
# --- 1. Without indent (compact) ---
json_string_compact = json.dumps(data)
print("--- Compact JSON ---")
print(json_string_compact)
print("\n" + "="*30 + "\n")
# --- 2. With indent=4 (pretty-printed) ---
json_string_pretty = json.dumps(data, indent=4)
print("--- Pretty-Printed JSON (indent=4) ---")
print(json_string_pretty)
Output:
--- Compact JSON ---
{"name": "John Doe", "age": 30, "isStudent": false, "courses": [{"title": "History", "credits": 3}, {"title": "Math", "credits": 4}], "address": {"street": "123 Main St", "city": "Anytown"}}
==============================
--- Pretty-Printed JSON (indent=4) ---
{
"name": "John Doe",
"age": 30,
"isStudent": false,
"courses": [
{
"title": "History",
"credits": 3
},
{
"title": "Math",
"credits": 4
}
],
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
As you can see, indent=4 creates a beautifully structured, easy-to-read output.

Common indent Values
While indent=4 is very common, you can use any positive integer.
indent=2: Often used for saving space while still being readable.indent=None: This is the same as not providing theindentargument at all. It produces the most compact output.
# Using indent=2
print("--- indent=2 ---")
print(json.dumps(data, indent=2))
Output:
--- indent=2 ---
{
"name": "John Doe",
"age": 30,
"isStudent": false,
"courses": [
{
"title": "History",
"credits": 3
},
{
"title": "Math",
"credits": 4
}
],
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
Combining indent with Other Useful Parameters
The json.dumps() function has other great parameters that work well with indent.
sort_keys=True
This sorts the keys of all dictionaries alphabetically. This is extremely useful for generating consistent JSON output, which is great for testing and diffing files.

print("--- indent=4 and sort_keys=True ---")
print(json.dumps(data, indent=4, sort_keys=True))
Output:
Notice how "address", "age", "courses", etc., are now in alphabetical order.
--- indent=4 and sort_keys=True ---
{
"address": {
"city": "Anytown",
"street": "123 Main St"
},
"age": 30,
"courses": [
{
"credits": 3,
"title": "History"
},
{
"credits": 4,
"title": "Math"
}
],
"isStudent": false,
"name": "John Doe"
}
separators
By default, json.dumps() adds a space after the colon () and comma (), like and . You can customize this with the separators parameter to save space.
It's a tuple: (item_separator, key_separator).
# Compact output with custom separators
print("--- Compact with custom separators ---")
print(json.dumps(data, separators=(',', ':')))
Output: This removes all the extra whitespace, making it even more compact than the default.
--- Compact with custom separators ---
{"name":"John Doe","age":30,"isStudent":false,"courses":[{"title":"History","credits":3},{"title":"Math","credits":4}],"address":{"street":"123 Main St","city":"Anytown"}}
Writing Pretty-Printed JSON to a File
For writing to a file, you should use json.dump() (no 's'). It writes the JSON object directly to a file-like object, which is more memory-efficient for large objects.
import json
data = {"name": "Jane Doe", "id": 123, "active": True}
# Open a file in write mode ('w')
# The 'with' statement ensures the file is automatically closed
with open('user.json', 'w') as f:
# Use json.dump() to write the pretty-printed JSON to the file
json.dump(data, f, indent=4)
print("Successfully wrote pretty-printed JSON to user.json")
If you open user.json, it will contain:
{
"name": "Jane Doe",
"id": 123,
"active": true
}
Reading and Re-formatting an Existing JSON File
What if you have an existing, unformatted JSON file and want to make it pretty? You can read it in and then write it back out with indent.
Let's say data.json contains:
{"name":"Peter Jones","hobbies":["reading","hiking"],"city":"New York"}
Here's the Python script to reformat it:
import json
# 1. Read the existing JSON file
with open('data.json', 'r') as f:
# Use json.load() to parse the JSON from the file into a Python object
data = json.load(f)
# 2. Write the object back to the file with pretty-printing
with open('data_pretty.json', 'w') as f:
json.dump(data, f, indent=4)
print("Successfully reformatted data.json into data_pretty.json")
The new file, data_pretty.json, will be nicely formatted:
{
"name": "Peter Jones",
"hobbies": [
"reading",
"hiking"
],
"city": "New York"
}
Summary Table
| Function | Purpose | indent Parameter |
|---|---|---|
json.dumps(obj) |
Converts a Python object to a JSON string. | indent=4 for pretty-printing. |
json.dump(obj, file) |
Writes a Python object to a file as JSON. | indent=4 for pretty-printing. |
json.loads(json_string) |
Parses a JSON string into a Python object. | Not applicable (for reading, not writing). |
json.load(file) |
Parses a JSON file into a Python object. | Not applicable (for reading, not writing). |
