dictutils.ops.deep_getΒΆ

dictutils.ops.deep_get(obj, path, default=None, *, strict=False)[source]ΒΆ

Get value from nested object using dotted path notation or path sequence.

Parameters:
  • obj (Any) – The object to access

  • path (Union[str, Sequence[Union[str, int]]]) – Path as string (β€œa.b.c”, β€œa[0].b”) or sequence [β€œa”, β€œb”, 0]

  • default (Any) – Value to return if path not found (when strict=False)

  • strict (bool) – If True, raise exception when path not found

Return type:

Any

Returns:

Value at the specified path, or default if not found

Example

>>> import json
>>> data = {"user": {"profile": {"name": "Alice", "tags": ["admin", "user"]}}}
>>> result = deep_get(data, "user.profile.name")
>>> print(json.dumps(result, indent=4))
"Alice"
>>> result = deep_get(data, ["user", "profile", "tags", 0])
>>> print(json.dumps(result, indent=4))
"admin"
>>> result = deep_get(data, "user.profile.age", default="unknown")
>>> print(json.dumps(result, indent=4))
"unknown"