Practice Bank: Working with APIs & JSON
A deeper set of practice problems, organized by topic, for extra reps
beyond the main exercises — including scenario-based problems
written in the same style you'll meet in real interviews. Every
"response" here is a hardcoded JSON string standing in for what a
real API would send back — no network access is needed or used
anywhere in this bank. No import beyond
json/os/re/math/datetime/random/csv.
starter.py below and run
python3 starter.py from inside the same folder.
Topic 1: What an API Is, and Why JSON
1 Parsing a JSON string
Find # TODO 1.1. Parse json_text with json.loads() and print its type and value.
2 Converting Python to JSON text
Find # TODO 1.2. Build a dict and convert it to a JSON string with json.dumps().
3 true/null becoming True/None
Find # TODO 1.3. Parse JSON containing true and null, and confirm what Python types they become.
4 Debug the Code
Find # TODO 1.4. This prints unparsed JSON text instead of parsing it first. Fix it.
Explaining why an app needs an API instead of hardcoded data
Find # TODO 1.A. Write explain_why_use_an_api().
Why JSON became the standard
Find # TODO 1.B. Write explain_why_json().
Topic 2: The requests Library Basics
1 Checking a status code
Find # TODO 2.1. Print success or failure based on status_code.
2 A mocked response.text
Find # TODO 2.2. Confirm the mocked text's type, then parse it.
3 Classifying status codes
Find # TODO 2.3. Write classify_status(code) covering success/client/server error ranges.
4 Debug the Code
Find # TODO 2.4. This compares a status code to the wrong type. Fix it.
Explaining requests.get() to a teammate
Find # TODO 2.A. Write explain_requests_get().
.text vs. .json()
Find # TODO 2.B. Write explain_text_vs_json().
Topic 3: The json Module: loads() & dumps()
1 Pretty-printing nested data
Find # TODO 3.1. Use json.dumps(nested, indent=2).
2 Parsing and summing values
Find # TODO 3.2. Parse compact JSON text and sum its values.
3 A dumps/loads round trip
Find # TODO 3.3. Confirm converting to JSON and back recovers the same data.
4 Debug the Code
Find # TODO 3.5. This passes indent="2" as a string. Fix it.
Saving an API response to a readable log file
Find # TODO 3.A. Write format_for_log(data).
When does indent= help?
Find # TODO 3.B. Write explain_indent_tradeoff().
Topic 4: Navigating Nested JSON
1 One level of nesting
Find # TODO 4.1. Read a value from a dict nested inside a dict.
2 A list nested inside a dict
Find # TODO 4.2. Read a value from a list of dicts.
3 Looping over nested records
Find # TODO 4.3. Loop over a list of order dicts.
4 Three levels deep
Find # TODO 4.4. Chain lookups through several nested layers.
5 Debug the Code
Find # TODO 4.5. This skips a required list-index step. Fix it.
Summing every line item on an invoice API response
Find # TODO 4.A. Write invoice_total(invoice).
Approaching an unfamiliar nested response
Find # TODO 4.B. Write explain_nested_navigation_approach().
Topic 5: Handling Errors: Bad Responses & Missing Keys
1 Returning None on failure
Find # TODO 5.1. Write safe_get_response(status_code, mock_body).
2 .get() with a default
Find # TODO 5.2. Safely read a possibly-missing key.
3 Classifying a failure
Find # TODO 5.3. Write classify_failure(status_code).
4 Catching a custom exception
Find # TODO 5.4. Catch a custom NetworkError.
5 Debug the Code
Find # TODO 5.5. This is missing a .get() default, causing a TypeError. Fix it.
Building a resilient weather summary
Find # TODO 5.A. Write summarize_weather(data).
Two failure modes
Find # TODO 5.B. Write explain_two_failure_modes().
Topic 6: Query Parameters & Headers
1 Building a query string
Find # TODO 6.1. Build a query string from a params dict.
2 Building an auth header
Find # TODO 6.2. Write build_auth_header(api_key).
3 Reading a missing environment variable
Find # TODO 6.3. Use os.environ.get() with a fallback.
4 A query string with a boolean value
Find # TODO 6.4. Build another query string, this time with a boolean param.
5 Debug the Code
Find # TODO 6.5. This hardcodes an API key. Fix it to read from an environment variable.
Building a search request URL
Find # TODO 6.A. Write build_search_url(query, limit).
Why an API key should never be hardcoded
Find # TODO 6.B. Write explain_api_key_safety().
Topic 7: Building a Pipeline: API → Data → Output
1 A fetch function
Find # TODO 7.1. Write fetch_mock_joke().
2 A parse function
Find # TODO 7.2. Write parse_joke(raw_text).
3 A format function
Find # TODO 7.3. Write format_joke(setup, punchline).
4 The full pipeline
Find # TODO 7.4. Chain fetch → parse → format together.
5 Debug the Code
Find # TODO 7.5. This parses data before fetching it. Fix the order.
A currency converter pipeline
Find # TODO 7.A. Write fetch_rate_data()/parse_rate()/format_conversion().
Why the fetch/parse/format split matters
Find # TODO 7.B. Write explain_pipeline_testability().
solution.py. Your exact
wording doesn't need to match — the goal is that your program
runs without errors and does what each TODO asks.