Skip to content

Fix single constant case #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jsonpath_ng/jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,11 @@ def find(self, datum):
datum = DatumInContext.wrap(datum)

# Used for catching null value instead of empty list in path
if not datum.value:
if datum.value is None:
return []
# Here's the hack. If it is a dictionary or some kind of constant,
# put it in a single-element list
if (isinstance(datum.value, dict) or isinstance(datum.value, int) or isinstance(datum.value, str)):
if (isinstance(datum.value, dict) or isinstance(datum.value, (int, float, str, bool))):
return self.find(DatumInContext([datum.value], path=datum.path, context=datum.context))

# Some iterators do not support slicing but we can still
Expand Down
5 changes: 5 additions & 0 deletions tests/test_jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_
# --------------------
#
("[*]", 1, [1], ["[0]"]),
("[*]", 1.2, [1.2], ["[0]"]),
("[*]", True, [True], ["[0]"]),
("[*]", False, [False], ["[0]"]),
("[*]", "test", ["test"], ["[0]"]),
("[*]", None, [], []),
("[0:]", 1, [1], ["[0]"]),
("[*]", {"foo": 1}, [{"foo": 1}], ["[0]"]),
("[*].foo", {"foo": 1}, [1], ["[0].foo"]),
Expand Down