Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I save Python data as readable, indented JSON?

Quick answer

Use Python’s json module to write your data to a separate output file opened in text mode with UTF-8 encoding. Pass indent=2 to json.dump to make the JSON easier to read. Then open that output file and use json.load to read it back. Choose an output filename that is not your source sample: opening a file in write mode erases its existing contents. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a small Python dictionary and want to save it as JSON that a person can read. What is the basic approach?

Geez

Import the standard json module, open a separate output file in text write mode with an explicit UTF-8 encoding, and pass the file to json.dump. Set indent=2 to put JSON members and array elements on separate, indented lines. Without an indentation setting, the JSON output has no inserted newlines. [S1]

For example, replace this sample data with your own Python structure:

import json

data = {
    "name": "Mina",
    "items": ["notebook", "pen"],
    "active": True,
}

# Choose an output filename separate from your source sample.
with open("result.json", "w", encoding="utf-8") as file:
    json.dump(data, file, indent=2)

The example writes to result.json; it does not open a source file. json.dump writes the serialized object to the writable file, and the explicit encoding is set when opening it. [S1]

Follow-up question

What does the saved file look like, and why use indentation?

Geez

For the example data above, the file content is:

{
  "name": "Mina",
  "items": [
    "notebook",
    "pen"
  ],
  "active": true
}

The indentation and line breaks make the nested structure and separate values easier to scan. The indent=2 setting asks the encoder to pretty-print object members and array elements. [S1]

Your file will contain the values in your own data variable. Let the JSON encoder format the output rather than trying to arrange the JSON by hand.

Follow-up question

How can I check that I can read the saved JSON back, and how do I avoid replacing my original sample?

Geez

Open the output file in read mode with UTF-8 encoding and pass its file object to json.load. Add this after the write step:

with open("result.json", "r", encoding="utf-8") as file:
    loaded_data = json.load(file)

print(loaded_data)

Use the same output path for this check that you used when writing. json.load reads JSON from a file object. [S2]

To preserve a source sample, give the output its own filename, such as result.json, rather than opening the source sample in 'w' mode. Write mode erases existing contents at the path being opened, so a separate output path avoids replacing that sample. [S1] [S2]

Follow-up question

What should I keep in mind when adapting the example to my own data?

Geez

Keep the write and reload steps pointed at the same output file, and choose a path distinct from any source sample you need to preserve:

import json

data = {"status": "ready", "count": 3}  # Replace with your data.
output_path = "result.json"  # Separate from any source sample.

with open(output_path, "w", encoding="utf-8") as file:
    json.dump(data, file, indent=2)

with open(output_path, "r", encoding="utf-8") as file:
    loaded_data = json.load(file)

The write step saves indented JSON; the read step loads it into a Python value. [S1] [S2] The reload check shows that the file can be read as JSON. It does not compare the loaded value with a separate expected result.

A practical checklist

  • Import Python’s json module.

  • Choose an output filename distinct from the source sample you want to preserve.

  • Open the output in text write mode with encoding="utf-8".

  • Call json.dump(data, file, indent=2) for readable formatting.

  • Reopen that output with UTF-8 encoding and use json.load to read it back.

A few more useful answers

Does `indent=2` change the data?

It controls the JSON’s formatting by adding indentation and line breaks for readability. [S1]

Why use a separate output filename?

Opening a file in write mode erases existing contents at that path. A distinct output path avoids overwriting the source sample. [S1] [S2]

What does the reload check tell me?

json.load reads JSON from a file object and returns the decoded Python value. It shows that the output can be loaded; it does not compare that value with a separate expected result. [S2]

Sources

References used for this guide.

  1. [S1] Python 3.15.2 Library Reference: json — JSON encoder and decoder
    docs.python.org · Checked
  2. [S2] Python Tutorial, section 7.2: Saving structured data with json
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work