Lesson 9 of 12
Structured learning draftLoad JSON and check its structure
A valid JSON document can still have the wrong shape for your program. Read a small settings document and check the type and required value before using it, so parsing success is not mistaken for application validity.
Learning objectives
- Parse a JSON document.
- Check its container and field types.
- Reject an invalid settings value.
Load JSON and check its structure: the working sequence
Trace the three stages of load json and check its structure and compare them with the example.
The text is parsed into a Python object.
The program requires a dictionary and an integer threshold.
Only a non-negative accepted threshold is displayed.
json.loads() turns JSON text into Python values. A document containing [] is valid JSON but is not a settings object. Keep the shape check and the business rule visible before using the data.
import json
settings = json.loads('{"reorder_level": 5}')
if not isinstance(settings, dict):
raise ValueError('Settings must be an object')
level = settings.get('reorder_level')
if type(level) is not int or level < 0:
raise ValueError('reorder_level must be a non-negative integer')
print(f'Reorder level: {level}')Reorder level: 5Try it, then change it
- Run the valid settings fixture.
- Try [] and explain why valid JSON is still rejected.
- Try a negative integer, a string '5' and true as the field value; explain the explicit integer check.
Common mistakes
- Treating successful parsing as proof of a valid configuration.
- Accepting Boolean true as a numeric threshold unintentionally.
- Using missing settings without defining a default policy.
Practice activity
Apply the lesson
Add a required shop_name string to the settings. Reject an empty or whitespace-only name and record the error for each invalid fixture. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
What does successful json.loads() establish?
Lesson summary
- Syntax, shape and business rules are separate checks.
- Validate values before they affect decisions.
- Use clear errors that identify the bad setting.
Sources and further reading
- json: JSON encoder and decoderPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.