Lesson 44/48 ยท ๐ŸŒ Working with the Real World
๐ŸŒ Working with the Real WorldLesson 44/48
Phase 8 ยท Working with the Real World20 min

JSON: The Language of APIs

json.loads(), json.dumps(), and working with nested data

JSON (JavaScript Object Notation) is THE format for web APIs, config files, and data exchange. Once you understand it, you can work with any REST API, Terraform state file, or K8s API response.
Parsing JSONpython
import json

# JSON string โ†’ Python dict
json_string = '{"name": "Alice", "age": 30, "active": true}'
data = json.loads(json_string)
print(data["name"])    # Alice
print(data["age"])     # 30
print(type(data))      # <class 'dict'>
Nested JSON (real API response)python
import json

response = '''
{
  "status": "success",
  "data": {
    "user": {"id": 42, "name": "Alice"},
    "permissions": ["read", "write", "admin"]
  }
}
'''

parsed = json.loads(response)
user_name = parsed["data"]["user"]["name"]
permissions = parsed["data"]["permissions"]
print(user_name)        # Alice
print(permissions[0])   # read
Python dict โ†’ JSON stringpython
import json

config = {
    "environment": "production",
    "replicas": 3,
    "features": ["logging", "monitoring"]
}

# Convert to JSON string
json_str = json.dumps(config, indent=2)
print(json_str)
๐ŸŒIn the Real World...

Terraform state is JSON. kubectl get pods -o json returns JSON. Every AWS API response is JSON. AWS Lambda functions send and receive JSON. If you know Python dicts, you know JSON.

Practice Exercises

0/3 solved
Exercise 1 of 3easy
โฑ 00:00

Parse API Response

Parse the JSON and print the user's name and email.
Expected output:Name: Alice SmithEmail: alice@example.com
solution.py
1 / 3
Exercise 2 of 3easy
โฑ 00:00

Extract Nested Data

Extract the region and instance type from nested JSON.
Expected output:Region: us-east-1Instance: t3.medium
solution.py
2 / 3
Exercise 3 of 3easy
โฑ 00:00

Build a JSON Config

Build a Python dict representing a deployment config and convert it to a JSON string.
Expected output (format must match):{"app": "myapp", "version": "2.0", "replicas": 3}
solution.py
3 / 3
Solve all 3 exercises to unlock completion