How should a Python application handle task data received from an API? JSON provides a readable text format for exchanging structured data between systems. An API response arrives as text; parse it before using it as Python data. section 9.3 section 9.3.3
A JSON object contains key–value pairs in curly braces, and a JSON array contains values in square brackets. Values can be strings, numbers, booleans, arrays, objects, or null. Object keys must be strings, and JSON strings require double quotes—not the single quotes Python also permits. Objects and arrays can be nested. section 9.3.1
Read an API response
```python import json response = '[{"title": "Laundry", "urgency": 3}, {"title": "Homework", "urgency": 5}]' tasks = json.loads(response) print(tasks[0]) # {'title': 'Laundry', 'urgency': 3} ``` `json.loads()` deserializes the JSON string into a Python list of dictionaries. It also accepts individual JSON values: `json.loads('false')` returns `False`, and `json.loads('null')` returns `None`. section 9.3.3
Parsed dictionaries are useful for accessing data, but they do not provide methods defined on a custom class. To work with task objects, a class method can construct each instance from a parsed dictionary using `cls(**task_dict)`. JSON numbers map to Python `int` or `float` according to their values. section 9.3.3 section 9.3.2
Send Python data as JSON
```python payload = ['text', False, {'0': None, 1: [1.0, 2.0]}] print(json.dumps(payload)) # ["text", false, {"0": null, "1": [1.0, 2.0]}] ``` `json.dumps()` serializes Python data to a JSON string. Notice the JSON spellings `false` and `null`, and the conversion of the numeric dictionary key to a string. A custom class instance is not serializable by default; provide encoding instructions, such as `json.dumps(task, default=lambda x: x.__dict__)`. Use `indent=2` for readable output and `sort_keys=True` when sorted keys are helpful. section 9.3.4
Use `loads` and `dumps` for JSON strings. For JSON files, the `json` module also provides `load` and `dump` with nearly identical calling signatures. section 9.3.5


































