Lab 09: Working with JSON and YAML
Objective
Practice reading, writing, and validating JSON and YAML documents. Convert between formats and work with environment variables.
Duration
20 to 30 minutes
Prerequisites
- Windows: Git Bash, WSL, or PowerShell. Python 3 installed.
- macOS: Terminal.app. Python 3 installed (
brew install python3or from python.org). - Linux: Any terminal. Python 3 installed (
sudo apt install python3).
Instructions
Part 1: JSON Validation
- Create a file called
server-config.json. Type it manually to build muscle memory:
{
"server": {
"host": "0.0.0.0",
"port": 3000,
"debug": false
},
"database": {
"engine": "postgres",
"host": "localhost",
"port": 5432,
"name": "bootcamp",
"credentials": {
"username": "admin",
"password": "changeme"
}
},
"features": {
"enableCache": true,
"maxConnections": 100,
"allowedOrigins": ["https://example.com", "https://app.example.com"]
}
}
- Validate it:
macOS / Linux / WSL / Git Bash:
python3 -m json.tool < server-config.json
Windows PowerShell:
Get-Content server-config.json | python -m json.tool
# Or use Python directly:
python -c "import json; json.load(open('server-config.json')); print('Valid JSON')"
- Intentionally break the JSON (remove a comma, use single quotes, add a trailing comma) and observe the error messages. Fix each one.
Part 2: YAML Conversion
- Create a file called
server-config.yamlthat represents the exact same data as the JSON above, but in YAML format:
server:
host: "0.0.0.0"
port: 3000
debug: false
database:
engine: postgres
host: localhost
port: 5432
name: bootcamp
credentials:
username: admin
password: changeme
features:
enableCache: true
maxConnections: 100
allowedOrigins:
- https://example.com
- https://app.example.com
- Validate your YAML:
macOS / Linux / WSL / Git Bash:
python3 -c "import yaml; print(yaml.safe_load(open('server-config.yaml')))"
Note: If you get
ModuleNotFoundError: No module named 'yaml', install it first:pip3 install pyyaml
Windows PowerShell:
python -c "import yaml; print(yaml.safe_load(open('server-config.yaml')))"
- Compare the two files: which is more readable? Which is shorter? Which supports comments?
Part 3: AWS IAM Policy (JSON)
- Create a file called
s3-read-policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::my-bootcamp-bucket",
"arn:aws:s3:::my-bootcamp-bucket/*"
]
}
]
}
-
Answer these questions by reading the policy:
- What actions are allowed?
- What resource(s) can be accessed?
- Can this policy delete objects? Why or why not?
-
Modify the policy to also allow
s3:PutObject(uploading files). Validate the JSON.
Part 4: Environment Variables
macOS / Linux / WSL / Git Bash
# Set environment variables
export APP_ENV="development"
export DB_HOST="localhost"
export DB_PORT="5432"
export SECRET_KEY="my-super-secret-key-123"
# Verify they're set
echo $APP_ENV
echo $DB_HOST
env | grep DB_
Windows PowerShell
# Set environment variables (session only)
$env:APP_ENV = "development"
$env:DB_HOST = "localhost"
$env:DB_PORT = "5432"
$env:SECRET_KEY = "my-super-secret-key-123"
# Verify
echo $env:APP_ENV
echo $env:DB_HOST
Get-ChildItem env: | Where-Object { $_.Name -like "DB_*" }
Windows Command Prompt (cmd)
set APP_ENV=development
set DB_HOST=localhost
echo %APP_ENV%
Read Variables in Python (all platforms)
Create read_env.py:
import os
env = os.getenv("APP_ENV", "production")
db_host = os.getenv("DB_HOST", "127.0.0.1")
db_port = os.getenv("DB_PORT", "5432")
print(f"Environment: {env}")
print(f"Database: {db_host}:{db_port}")
Run it:
python3 read_env.py # macOS / Linux / WSL
python read_env.py # Windows
Now unset APP_ENV and run again to see the default value:
macOS / Linux / WSL:
unset APP_ENV
python3 read_env.py
Windows PowerShell:
Remove-Item env:APP_ENV
python read_env.py
Validation
- Created and validated
server-config.json(and fixed intentional errors) - Converted to
server-config.yamland validated - Read and modified an IAM policy JSON document
- Set, read, and used environment variables on your platform
- Ran a Python script that reads environment variables with defaults
AWS Bootcamp: From Novice to Architect Author: Samuel Ogunti License: CC BY-NC 4.0