Read Nested Data with a Dotted Path

easy
Unit 4 · Dicts0/3 to milestone

API responses and LLM tool outputs are deeply nested JSON. Write a safe accessor: get_path(data, "user.addresses.0.city", default=None).

  • Split the path on .. For dicts, look up the key. For lists, the segment must be an integer index (negative indices allowed).
  • If anything along the way is missing, out of range or the wrong type, return default instead of raising.
  • An empty path returns data itself.

Then implement set_path(data, path, value), which sets a value and creates missing dicts along the way. Numeric segments into existing lists set by index. set_path modifies data in place and returns it.

#dicts#json#parsing2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Read Nested Data with a Dotted Path · TechGradify