PythiloLesson 6 of 8 · progress kept in this browser

Validate a decoded JSON value at the boundary

Decoding JSON proves that the text is valid JSON, not that its shape is suitable for your program. A boundary validator should reject the wrong container, missing fields and misleading scalar types. In Python, bool is a subclass of int, so isinstance alone may accidentally accept True as a numeric identifier. State those rules explicitly.

import json
value = json.loads('{"count": true}')
print(type(value["count"]) is int)
Your task

Write parse_count(text). Decode a JSON object and return its count only if that value has exactly type int and is nonnegative. Raise ValueError for invalid JSON, a wrong shape, a missing key, bool, or any other count type. Ignore other keys.