Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
How is nested JSON represented after json.loads()?
Show the answer
As nested Python dictionaries and lists.
Decode a response; do not confuse absence with success
These are mock JSON strings, not HTTP requests. json.loads converts JSON into Python data; parsing alone does not validate the response shape. The trace tasks assume a JSON object with status text and a list of records containing item_id. Test status and each record before using a real service.
dictionary.get("data", []) supplies a fallback only when the key is missing. A present null becomes None, not an empty list. The change task assumes data, when present, is a list. The extract_records filter returns a list for status ok and [] for non-ok envelopes; [] alone cannot tell a failed request from a successful empty result. Keep the original status/error evidence when integrating a service.
The independent helper validates its text/object/list boundary, leaves malformed JSON errors visible and starts fresh per call. The complete example keeps status beside the list so the caller can distinguish error from empty success. Neither local code nor tutor login sends an automatic API request.
# Preserve response status beside the typed records.
import json
def decode_response(text):
response = json.loads(text)
if not isinstance(response, dict):
raise ValueError("Expected a JSON object")
status = response.get("status")
rows = response.get("data", [])
if not isinstance(rows, list):
raise ValueError("Expected a data list")
return status, rows
for text in [
'{"status":"ok","data":[]}',
'{"status":"error","message":"timeout"}',
]:
status, rows = decode_response(text)
print(status, len(rows))
Both results have zero records, but only one reports ok. Preserve the envelope when interpreting absence.
Refresh JSON parsing and typed validation on Days 21 and 24. A small extraction filter is not a network reliability policy.
Predict, explain, then test
These three comparisons are self-reviewed, not scored. Coding checks run new inputs; the section quiz records separate knowledge evidence.
Name the boundary
Does successful JSON parsing prove data is a list?
Compare your answer · self-reviewed
No. Parsing produces Python data; validate the object and field types separately.
Inspect a missing key
Does get("data", []) replace a present null with []?
Compare your answer · self-reviewed
No. Present null becomes None. The fallback applies only when the key is absent.
Preserve status
Can zero records distinguish ok/empty from a failed mock envelope?
Compare your answer · self-reviewed
No. Keep status/error evidence rather than interpreting every empty list as success.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.