Lab — Python JSON Validator¶
Lab Overview¶
Purpose: Validate JSON config/payloads used by automation without calling external APIs.
Scenario: A webhook payload and a service.json manifest must pass schema checks before a pipeline continues.
Expected outcome: CLI exits 0 on valid JSON matching rules, 2 on parse or schema failure.
This is a lab, not a tutorial
Business Scenario¶
CI receives service.json:
Rules: name non-empty string, port int 1–65535, replicas int ≥ 1.
Learning Objectives¶
- Parse JSON with
json.load/json.loadsand handleJSONDecodeError - Validate types and ranges defensively
- Use pathlib for file I/O
- Exit
0/2consistently with YAML lab conventions
Prerequisites¶
Knowledge¶
- File Handling — pathlib, JSON, YAML, CSV
- Error Handling and Exceptions
- CLI Applications — argparse, Click, Typer
Software¶
Python 3.12+ (stdlib json only). Estimated cost: £0.
Environment¶
mkdir -p ~/rebash-lab-python-json/{ok,bad}
cd ~/rebash-lab-python-json
python3 -m venv .venv && source .venv/bin/activate
Initial State¶
cat > ok/service.json << 'EOF'
{"name": "checkout", "port": 8080, "replicas": 2}
EOF
cat > bad/service.json << 'EOF'
{"name": "", "port": 99999, "replicas": 0}
EOF
echo 'not-json' > bad/broken.json
Task¶
Create validate_json.py with positional path(s). Load JSON, collect all schema errors, print to stderr, exit 2 on any failure.
Validation¶
python validate_json.py ok/service.json; echo $? # 0
python validate_json.py bad/service.json; echo $? # 2
python validate_json.py bad/broken.json; echo $? # 2
- Valid exits
0 - Schema and parse failures exit
2 - Multiple errors reported when possible
Troubleshooting¶
| Symptom | Fix |
|---|---|
True accepted as int | Reject bool explicitly (isinstance(x, bool)) |
| Unicode errors | Open with encoding="utf-8" |
Cleanup¶
Production Discussion¶
For evolving contracts, prefer JSON Schema (Draft 2020-12) or Pydantic models shared between producers and consumers.
Related¶
- Previous: YAML Config Validator
- Next: GitHub Repository Auditor