Coding Challenges: Working with APIs & JSON
8 HackerRank-style challenges, auto-graded right in your browser —
no setup required. Write your function in the code box, then click
Run Tests to check it against a set of test cases
(including edge cases). Stuck? Every challenge has a
Show Solution reveal at the bottom. Every challenge
takes a JSON string or plain data as input — none of them need a
live network call. Only uses what Chapters 1-19 have covered so far.
No import beyond json/os/re/math/datetime/random/csv.
1 Classify a Status Code Easy
Write a function parse_response_status(status_code) that returns "success" if 200 <= status_code < 300, "client_error" if 400 <= status_code < 500, "server_error" if 500 <= status_code < 600, and "unknown" for anything else.
status_code is always an integer.
parse_response_status(200) → "success"parse_response_status(404) → "client_error"
Show Solution
def parse_response_status(status_code):
if 200 <= status_code < 300:
return "success"
elif 400 <= status_code < 500:
return "client_error"
elif 500 <= status_code < 600:
return "server_error"
return "unknown"
2 Read One Field From JSON Text Easy
Write a function parse_json_field(json_text, field) that parses json_text with json.loads() and returns the value of field, or None if that field isn't present.
json_text is always valid JSON representing a flat object.
parse_json_field('{"city": "Austin"}', "city") → "Austin"parse_json_field('{"city": "Austin"}', "humidity") → null
Show Solution
import json
def parse_json_field(json_text, field):
data = json.loads(json_text)
return data.get(field)
3 Pretty-Print JSON Easy
Write a function pretty_print_json(data) that returns data (a Python dict) converted to an indented JSON string, using json.dumps(data, indent=2).
data is always a JSON-serializable dict.
pretty_print_json({"a": 1}) → '{\n "a": 1\n}'
Show Solution
import json
def pretty_print_json(data):
return json.dumps(data, indent=2)
4 Get a Deeply Nested Value Medium
Write a function get_nested_value(data, keys) that navigates data (a nested dict/list structure) one step at a time for every key or index in the list keys, and returns whatever value is reached at the end.
keys may contain a mix of dict keys (strings) and list indexes (integers); the full path is always valid for the given data.
get_nested_value({"a": {"b": {"c": 42}}}, ["a", "b", "c"]) → 42get_nested_value({"items": [{"name": "x"}, {"name": "y"}]}, ["items", 1, "name"]) → "y"
Show Solution
def get_nested_value(data, keys):
current = data
for key in keys:
current = current[key]
return current
5 Safe Field Access Easy
Write a function safe_field(data, key, default) that returns data[key] if present, otherwise returns default, using dict.get().
data is always a flat dict.
safe_field({"a": 1}, "a", 0) → 1safe_field({"a": 1}, "b", 0) → 0
Show Solution
def safe_field(data, key, default):
return data.get(key, default)
6 Build a Query String Medium
Write a function build_query_string(params) that builds a query string from the dict params, in the form "key1=value1&key2=value2", preserving the dict's insertion order. Return an empty string if params is empty.
params values may be strings, integers, or booleans.
build_query_string({"a": 1, "b": 2}) → "a=1&b=2"build_query_string({}) → ""
Show Solution
def build_query_string(params):
return "&".join(f"{k}={v}" for k, v in params.items())
7 Sum a Field Across a List of Records Medium
Write a function sum_nested_field(records, key) that returns the sum of record[key] across every dict in the list records. Return 0 if records is empty.
records is guaranteed to have key.
sum_nested_field([{"amount": 10}, {"amount": 20}], "amount") → 30sum_nested_field([], "amount") → 0
Show Solution
def sum_nested_field(records, key):
return sum(record[key] for record in records)
8 Fetch-Parse-Format Mini Pipeline Medium
Write a function fetch_parse_format(raw_json_text, template) that parses raw_json_text with json.loads(), then returns template.format(**data), filling in the template's placeholders from the parsed dict's keys.
template's placeholder names always match keys present in the parsed data.
fetch_parse_format('{"name": "Ada", "score": 99}', "{name} scored {score}") → "Ada scored 99"
Show Solution
import json
def fetch_parse_format(raw_json_text, template):
data = json.loads(raw_json_text)
return template.format(**data)