Serialization converts native Python objects or values into JSON data for an outgoing communication route. The json module provides dumps for producing a JSON string and dump for writing JSON directly to a file. section 9.3.4 section 9.3.5
For built-in data, json.dumps performs predictable conversions:
- Python strings become JSON strings with double quotes; False becomes false; None becomes null. section 9.3.4
- Dictionary keys become JSON strings, so the Python key 1 becomes the JSON key "1". section 9.3.4
- Tuples are JSON-serializable and become JSON arrays after serialization. section 9.3.6
- Custom class instances are not JSON-serializable by default because the encoder cannot know which attributes or metadata to include. section 9.3.4
Serializing a custom object
A fallback encoder can convert a Task instance through its __dict__ representation: dumped_task = json.dumps(tasks[0], default=lambda x: x.__dict__) print(dumped_task) # {"title": "Laundry", "desc": "Wash clothes", "urgency": 3} The default argument supplies instructions for objects the encoder cannot handle. Choose the representation deliberately, because it determines which object data crosses the system boundary. section 9.3.4
Readable output can use indent=2, while sort_keys=True produces stable alphabetical key ordering. JSON serialization and deserialization form opposite directions: Python data becomes JSON, and JSON becomes native Python data. section 9.3.4 section 9.6























