1. JSON Serialization & Deserialization
dumps/loads vs. dump/load
- `json.loads(str)`: Deserializes a JSON string into Python dictionaries, lists, booleans, and numbers.
- `json.dumps(obj)`: Serializes a Python data structure into a JSON-formatted string.
- Methods without the "s" (`json.load(file)` and `json.dump(obj, file)`) work directly with file streams.
import json
student = {"name": "Grace", "enrolled": True, "gpa": 3.88}
# Serialize to formatted string
json_str = json.dumps(student, indent=2)
print("JSON output:\n" + json_str)
# Deserialize back
obj = json.loads(json_str)
print("Parsed name:", obj["name"])
💡 Key Takeaway: The "s" in json.dumps/loads stands for String.