Command Palette

Search for a command to run...

Hectal
Phase 7: Errors, Files and Data Formats
Python · Exercise 5 of 7 PreviousNext
Intermediate

Save and load JSON safely

Write save_json(path, data) that writes data to the file as JSON with indent=2, in UTF-8, keeping non-English letters as they are (ensure_ascii=False).

Write load_json(path, default=None) that reads the JSON back. It returns default if the file does not exist or does not contain valid JSON.

What Check tests

  1. A round trip returns the same data
  2. A missing file gives the default
  3. Invalid JSON gives the default
  4. Letters like é are written as they are
  5. An empty file gives the default
  6. A stored falsy value is returned, not the default

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.