h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
572 stars 85 forks source link

Allow filtering dictionaries using entry keys #74

Closed languitar closed 2 months ago

languitar commented 3 years ago

Before this commit, it was not possible to filter on the value of object entries based on their keys such as in the following example

Given the input

{"foo": 42"}

The JSONPath $[?(@.foo==42)] did not produce any results and therefore is was only possible to access top-level dictionary entries, but not to produce or not to produce a result at all based on the value of such a top-level entry.

This commit implements this feature while trying to keep up all other working path expressions known from the test cases.

Interestingly, one test case specifically tried to prevent these semantics (at a deeper level of the document structure). This case had to be removed to make the test suite pass again. I could not find the underlying issue number 2 to find out why this test case existed as that issue seems to stem from a parent of this fork from long ago. Maybe, a resulting release should therefore receive a major version bump as this might be understood as a breaking change.

In general, the specification is pretty lose on how square brackets and filter expression are to be understood in the case of dictionaries, not lists. The implementations here follows the already existing practical approach that simply makes more expressions and intended use cases working. However, this results in multiple paths being valid to access the same objects and therefore an ambiguity is created.

Fixes #66