I'd like to know if there is an existing Python library for filtering JSON documents by full key path that preserves the document's original structure. Here's an example:
Inputs
d = {"a": {"b": "c", "d": "e"}, "f": [{0: 0}, {1: 1}]}
keyPaths = ["a.b", "f[1]"]
Desired Output
{"a": {"b": "c"}, "f": [{1: 1}]}
The closest solution I have found is the jmespath MultiSelect Hash operation; however, I don't think it can create the full hierarchy. For instance:
jmespath.search("{a: a.b, f: f[1]}", d)
yields {'a': 'c', 'f': {1: 1}}
But it doesn't allow a.b
as a keyval-expr's identifier i.e., {a.b: a.b}
is invalid. One solution would be to use the MultiSelect Hash along with code to generate the missing structure for each key path. I'm wondering though if there's a more elegant solution.