Python 24-Day Course - Day 17: JSON Processing

Day 17: JSON Processing

What Is JSON?

JSON stands for JavaScript Object Notation, a widely used text format for data exchange.

Python Objects and JSON Conversion

import json

# Python -> JSON string
data = {
    "name": "Alice",
    "age": 25,
    "hobbies": ["coding", "reading"],
    "address": {"city": "Seoul", "zip": "06000"}
}

json_str = json.dumps(data, ensure_ascii=False, indent=2)
print(json_str)

# JSON string -> Python
parsed = json.loads(json_str)
print(parsed["name"])       # Alice
print(parsed["hobbies"][0]) # coding

Type Mapping

PythonJSON
dictobject {}
listarray []
strstring
int, floatnumber
True / Falsetrue / false
Nonenull

Reading/Writing JSON Files

# Save to file
with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

# Read from file
with open("data.json", "r", encoding="utf-8") as f:
    loaded = json.load(f)
    print(loaded["address"]["city"])  # Seoul

Working with Complex JSON

# API response example
api_response = """
{
  "status": "success",
  "results": [
    {"id": 1, "title": "Python Intro", "price": 25000},
    {"id": 2, "title": "Django in Practice", "price": 35000},
    {"id": 3, "title": "FastAPI Guide", "price": 30000}
  ]
}
"""

data = json.loads(api_response)
for book in data["results"]:
    print(f"{book['title']}: ${book['price']:,}")

# Filtering and sorting
expensive = sorted(
    [b for b in data["results"] if b["price"] >= 30000],
    key=lambda b: b["price"],
    reverse=True
)

Today’s Exercises

  1. Write code that reads a config file (config.json) and applies it to a program.
  2. Create CRUD functions that save list data to a JSON file and read it back.
  3. Write a function that recursively finds the value of a specific key in nested JSON.

Was this article helpful?