Level 2 / Project 10 - Mock API Response Parser¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | Diagram | Browser |
Estimated time: 40 minutes
Focus¶
- parse response-like payloads
Why this project exists¶
This project gives you level-appropriate practice in a realistic operations context. Goal: run the baseline, alter behavior, break one assumption, recover safely, and explain the fix.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-2/10-mock-api-response-parser
python project.py data/sample_input.txt
python project.py data/sample_input.txt --validate status data message
python project.py data/sample_input.txt --group role
pytest -q
Expected terminal output¶
Status: {'status': 200, 'category': 'success'}
{"count": 5, "fields": ["active", "id", "name", "role"], ...}
11 passed
Expected artifacts¶
- Parsed response summary on stdout
- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a
--filterflag:--filter active=trueto show only matching items. - Add support for paginated responses (read
pagination.total_pages). - Add a
--status-onlyflag that just checks and prints the status category.
Break it (required) — Core¶
- Feed a file with a JSON array at the root instead of an object.
- Feed a response where
datais a string instead of a list. - Pass a response with no
statusfield at all.
Fix it (required) — Core¶
- Handle non-object JSON roots (wrap arrays in a response dict).
- Guard against non-list data values in
extract_items. - Return a clear message when status code is missing.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- What makes a JSON response "valid" beyond just being parseable JSON?
- How do HTTP status code ranges (2xx, 4xx, 5xx) map to outcomes?
- Why is
dict.get(key, default)safer thandict[key]for API data? - What is the pagination pattern and why do APIs use it?
Mastery check¶
You can move on when you can: - parse a nested JSON response and extract specific fields, - validate response structure against a schema, - categorise HTTP status codes from memory, - handle missing or malformed API data gracefully.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|