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.
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.
mkdir -p ~/devops-python/lesson-301cd ~/devops-python/lesson-301source ~/devops-python/lesson-101/devops-env/bin/activate{
"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"]
}python3 -c "import json; data = json.load(open('deploy.json')); print(type(data)); print(list(data.keys()))"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.
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.