util.dicts.get
Used to get the value of a path in a dictionary. If the path doesn't exist, it returns None
. The path is a string where nested keys are separated by periods. Array items can also be referenced by index using square brackets.
Arguments:
def util.dicts.get(
d: dict = None,
path: str = None,
):
Returns a value of any type or None.
Example:
d = { "foo": { "bar": [{"car": 2}] } }
path = "foo"
test = util.dicts.get(d=d, path=path) # { "bar": [{"car": 2}] }
path = "foo.bar"
test = util.dicts.get(d=d, path=path) # [{"car": 2}]
path = "foo.bar[0]"
test = util.dicts.get(d=d, path=path) # {"car": 2}
path = "foo.bar[0].car"
test = util.dicts.get(d=d, path=path) # 2
path = "foo.bar[0].moo"
test = util.dicts.get(d=d, path=path) # None
path = "foo.bar[1].car"
test = util.dicts.get(d=d, path=path) # None