What can go wrong when a Python result crosses a JSON API boundary?
Understand the idea, explain it in your own words and use the rubric to find your next learning gap.
The idea behind the question
Python values and JSON values are not identical. Dates, sets, non-finite floats and very large inputs need a deliberate serialization and validation policy.
A model-extraction response includes a datetime object, a set of tags and a NaN confidence value.
import json
payload = {"name": "Subo", "active": True, "note": None}
encoded = json.dumps(payload, ensure_ascii=False)
print(encoded)
print(json.loads(encoded)["note"] is None)
try:
json.dumps({"score": float("nan")}, allow_nan=False)
except ValueError:
print("Rejected non-finite number")Show expected output
{"name": "Subo", "active": true, "note": null}
True
Rejected non-finite numberCopy and paste to run in a separate tab. This example illustrates one concept; it is not a complete application or an automatically graded task.
A practical way to learn it
Run the JSON example, then add a date field with an explicit ISO-format contract and a test for a missing value.
Open Python playgroundThe related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.
Build an explanation you understand
- State what the concept does in plain language.
- Apply it to the example and make your assumptions explicit.
- Explain a trade-off, failure case and how you would test it.
- Distinguish your actual experience from a proposed approach.
How would you explain it?
Single-question timers reset on refresh. Full mock sessions can resume from this browser.
Strengthen your explanation
Compare reasoning, not identical wording. Reference answers describe a possible approach; replace hypothetical statements with truthful details from your own work.
One way to answer
I would define the wire contract rather than serialize arbitrary Python objects implicitly. I would convert dates to an agreed textual format, represent sets as an ordered list when order matters and reject non-finite numbers for strict JSON. I would distinguish a missing field from an explicit null and validate incoming structure and size. I would not use default=str as a universal fix because it can hide an unintended type. A round-trip test should check meaning and types, including Unicode, instead of only whether json.dumps returned a string.
What a strong explanation should cover
Tick only the points your own answer addresses. This is self-assessment, not AI scoring.
0 / 3 points self-reportedWatch out for
- Converting every unknown object to a string and calling the contract valid.
Now go one level deeper
- How would you represent an absent date?
- Why should the request body have a size limit?
Further reading
Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.