DataCareerHub
Explore learning
← Shared foundationsFree companion · 25 minute suggested activity

Validate an API response before analysis

HTTP, JSON & API contracts · Shared foundation workshop · Separate transport success from a valid response contract.

Learn → Try → ExplainReviewed 2026-10-15
01

Objective & prerequisites

Separate transport success from a valid response contract. Prerequisites: basic Python expressions and files; use a disposable local practice folder and fictional data. This shared workshop complements the existing data/AI tracks and does not create a second progress system.

02

Understand the concept

JSON is a data representation. A response can be syntactically valid JSON and still violate required fields, types, bounds or business semantics. Validate the envelope and each row, reject unexpected keys when the contract is closed, then check unique identifiers and nonnegative integer money. Authentication and rate limits are separate service responsibilities.

03

Worked example

validated_rows accepts only {rows:[...]}, with unique text IDs and integer nonnegative cents. It rejects an extra field, a duplicate ID or True used as money. The lab operates on local dictionaries; it is explicitly a mock of the contract boundary, not an external API integration.

from foundation_labs import validated_rows
rows=validated_rows({"rows":[{"id":"A","amount_cents":30000}]})
assert rows[0]["amount_cents"]==30000
04

Try it yourself

Run the positive and duplicate tests. Add an unknown top-level key in a local copy and explain why silently ignoring it could hide a mismatched API version. Record your prediction before running the supplied reference, then compare the observed result and explain any difference.

Worked solution

Two rows valued 30000 and 40000 yield 70000 cents. DuplicateA is rejected before aggregation. Validating only json.loads would not protect the unique-key or numeric-type contract.

06

Knowledge check

Does HTTP200 prove the JSON matches your analytical schema? Try answering without rereading the example.

Check and explain

No. Validate both the service response meaning and the data contract before using its values.

08

Mini-project, mistakes & handover

Produce a short README with the question, exact local command, input meaning, checks and actual results. Include one failure case and its explanation. Never silently turn an exception into a passing result. Mark simulated behavior explicitly, credit the supplied teaching code and identify your own contribution. These activities are unsaved self-practice, not certification.

09

Authoritative reference

Read the official reference ↗

— consulted for the underlying interface/concept. The lesson and fictional examples are original teaching material. Python reference tested with 3.12; designed for 3.10+. Git lab requires Git 2.28+.

Make the reasoning yours.

Try the activity before reading its reference. Keep your sources, commands and assumptions with your work. Companion activities are unsaved self-practice and do not award a certificate or change lesson progress.

Resume guidance and examples are for learning and preparation only. They do not guarantee interviews, a job offer or employment.

Public registration, payments and new paid enrolment remain disabled. These free resources do not grant access to protected course modules.

Learner dashboard ↗ · Ask a question ↗