301 — Reading and Writing JSON Config Files

Beginner

Use Python's built-in `json` module to parse config files, serialize Python objects to JSON, handle malformed input safely, and build a reusable config loader — the same pattern used by deployment scripts that read environment config from JSON files.

Learning Objectives

1
Create a JSON config file and read it into a Python dict with json.load()
2
Parse a JSON string with json.loads() to simulate handling an API response body
3
Serialize a Python dict to a JSON string with json.dumps() and control indentation and key ordering
4
Write a Python dict to a JSON file with json.dump()
5
Catch json.JSONDecodeError to handle malformed or missing config files without crashing
6
Update a JSON file in place using a read-modify-write pattern
7
Build a deploy_config.py CLI tool that reads a multi-environment JSON config, validates required keys, and exits with a clear error if any are missing
Step 1

Set up the lesson 301 workspace and create a JSON config file

Create the lesson-301 directory, activate the virtual environment, and write a `deploy.json` config file by hand. This file represents a multi-environment deployment config — a common structure in DevOps tooling.

Commands to Run

mkdir -p ~/devops-python/lesson-301
cd ~/devops-python/lesson-301
source ~/devops-python/lesson-101/devops-env/bin/activate

Files for This Step

deploy.jsonPaste this into the file
{
  "app_name": "devops-app",
  "version": "1.3.0",
  "environments": {
    "dev": {
      "host": "dev.example.internal",
      "port": 8080,
      "debug": true,
      "replicas": 1
    },
    "staging": {
      "host": "staging.example.internal",
      "port": 8080,
      "debug": false,
      "replicas": 2
    },
    "prod": {
      "host": "app.example.com",
      "port": 443,
      "debug": false,
      "replicas": 5
    }
  },
  "notify_on_failure": ["ops@example.com", "platform@example.com"]
}

After Saving

python3 -c "import json; data = json.load(open('deploy.json')); print(type(data)); print(list(data.keys()))"

What This Does

The json module is part of the Python standard library — no pip install is needed.

JSON is the most common data format exchanged between DevOps tools, APIs, and config files.

The structure here reflects a real deployment config: a top-level app block plus an environments dict that holds per-environment settings.

JSON objects ({}) become Python dict, JSON arrays ([]) become Python list, JSON true/false become Python True/False, and JSON numbers become int or float.

This type mapping is exact and reversible — the same object that comes out of the parser can be written back to produce identical JSON.

Expected Outcome

The one-liner prints <class 'dict'> and ['app_name', 'version', 'environments', 'notify_on_failure'] — confirming the file was parsed and the top-level keys are accessible.

Pro Tips

  • 1
    The `json` module is in the standard library for all Python 3 versions — no install step is ever needed.
  • 2
    JSON requires double quotes for strings. A config file using single quotes is not valid JSON and will raise `json.JSONDecodeError` when you try to load it.
Was this step helpful?

All Steps (0 / 8 completed)