Python json Module

The json module provides functions for encoding and decoding JSON (JavaScript Object Notation) data. JSON is the most common data interchange format for web APIs, configuration files, and data storage.

Use Cases:
- Reading JSON configuration files
- Serializing Python objects to JSON strings for API responses
- Parsing JSON responses from HTTP requests
- Pretty-printing JSON for debugging
- Custom serialization of complex objects like datetime or Decimal

Example 1: Reading and writing JSON files
    import json
    from pathlib import Path

    # Write
    config = {"host": "localhost", "port": 8080, "debug": True}
    Path("config.json").write_text(json.dumps(config, indent=2))

    # Read
    data = json.loads(Path("config.json").read_text())
    print(data["host"])  # localhost

Example 2: Custom serialization
    import json
    from datetime import datetime

    class DateEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, datetime):
                return obj.isoformat()
            return super().default(obj)

    event = {"name": "Launch", "date": datetime(2025, 6, 1)}
    print(json.dumps(event, cls=DateEncoder, indent=2))

Key functions include json.dumps (object to string), json.loads (string to object), json.dump (object to file), and json.load (file to object). The module handles Python dicts, lists, strings, ints, floats, booleans, and None, mapping them to their JSON equivalents.
