JSON Handling: Parse, Generate, and Persist Structured Data

Introduction

In this chapter, you will learn JSON handling in Python using the built-in json module. JSON is one of the most common data formats in APIs, configuration files, and data exchange. Once you understand JSON operations, your Python programs can communicate with real systems much more effectively.

Prerequisites

  • Python 3.10+ installed
  • Basic understanding of dictionaries, lists, strings, and file handling
  • Ability to run .py files in terminal or IDE

What Is JSON

JSON (JavaScript Object Notation) is a lightweight text format for structured data.

In Python mapping terms:

  • JSON object <-> Python dict
  • JSON array <-> Python list
  • JSON string <-> Python str
  • JSON number <-> Python int / float
  • JSON boolean <-> Python True / False
  • JSON null <-> Python None

Example JSON text:

json
{
  "name": "Emma",
  "age": 12,
  "skills": ["python", "math"]
}

1) Convert Python Object to JSON String (dumps)

Use json.dumps() when you need JSON text in memory.

python
import json
 
# Python object
student = {
    "name": "Emma",
    "age": 12,
    "skills": ["python", "math"]
}
 
# Convert to JSON string
json_text = json.dumps(student)
print(json_text)

Pretty-print JSON:

python
import json
 
student = {"name": "Emma", "age": 12, "skills": ["python", "math"]}
pretty = json.dumps(student, indent=2, ensure_ascii=False)
print(pretty)

2) Convert JSON String to Python Object (loads)

Use json.loads() to parse JSON text.

python
import json
 
json_text = '{"name": "Liam", "score": 95}'
data = json.loads(json_text)
 
print(data)          # dict
print(data["name"])  # Liam

3) Write JSON to File (dump)

Use json.dump() to store JSON data in files.

python
import json
 
scores = {
    "Emma": 95,
    "Liam": 88,
    "Noah": 92
}
 
# Write JSON file
with open("scores.json", "w", encoding="utf-8") as f:
    json.dump(scores, f, indent=2, ensure_ascii=False)

4) Read JSON from File (load)

Use json.load() to read file content as Python objects.

python
import json
 
with open("scores.json", "r", encoding="utf-8") as f:
    scores = json.load(f)
 
print(scores)
print(scores["Emma"])

Tip

Best Practice

When writing JSON files, usually use indent=2 for readability and ensure_ascii=False for better multilingual text output.

5) Error Handling for Invalid JSON

Parsing bad JSON may raise json.JSONDecodeError.

python
import json
 
bad_json = '{"name": "Emma", "age": 12,}'  # trailing comma is invalid
 
try:
    data = json.loads(bad_json)
except json.JSONDecodeError as e:
    print(f"Invalid JSON: {e}")

This is very important for robust API/file input handling.

6) Real Mini Example: Student Score JSON Report

This example saves student records to JSON, then loads and summarizes.

python
import json
 
# Student records
students = [
    {"name": "Emma", "score": 95},
    {"name": "Liam", "score": 88},
    {"name": "Noah", "score": 92}
]
 
# Save to JSON file
with open("student_scores.json", "w", encoding="utf-8") as f:
    json.dump(students, f, indent=2, ensure_ascii=False)
 
# Read back from JSON file
with open("student_scores.json", "r", encoding="utf-8") as f:
    loaded_students = json.load(f)
 
# Compute average score
avg = sum(item["score"] for item in loaded_students) / len(loaded_students)
 
print("Loaded data:", loaded_students)
print(f"Average score: {avg:.2f}")

This pattern appears in API caching, local config, and report pipelines.

When debugging JSON visually, you can use this formatter tool:

It helps quickly validate structure and readability during development.

Warning

JSON keys and string values must use double quotes ("), not single quotes (').
Also avoid trailing commas in objects/arrays.

Common Beginner Mistakes

Mistake 1: Confusing dump and dumps

dump writes to file; dumps returns JSON string.

Mistake 2: Confusing load and loads

load reads from file object; loads parses JSON string.

Mistake 3: Using Non-Serializable Objects Directly

Types like custom class instances cannot be dumped without custom conversion logic.

Surprise Practice Challenge

Build a "To-Do JSON Manager":

  1. Store task list as JSON file
  2. Support add task, mark done, list tasks
  3. Save after each operation
  4. Reload tasks when app starts
  5. Handle invalid file/JSON gracefully

If you finish this, you can build real local-data CLI tools with JSON persistence.

FAQ

Should I use JSON or CSV for beginner projects?

Use JSON when data has nested structure; use CSV for flat table-like data.

Why does my parsed value type change?

JSON parsing maps data into Python-native types based on JSON value types.

Can JSON store comments?

Standard JSON does not support comments.

Is JSON good for very large datasets?

For very large data, stream processing or binary formats may be more efficient, but JSON is still excellent for readability and interoperability.