Skip to content
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

filter.Expresson.find() does not handle 'None' values correctly #153

Open
code-inflation opened this issue Dec 7, 2023 · 3 comments
Open
Labels

Comments

@code-inflation
Copy link

find() doesn't seem to be able to handle None values.

Example:

from jsonpath_ng.ext.parser import parse

data = [{"test": True}, {"test": None}]

query = $[?(test == true)]
parser = parse(query)
parser.find(data)

Causes the following error:

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
@amittendulkar
Copy link

Minor correction above. Instead of ,
query = $[?(test == true)]
should be,
query = '$[?(test == true)]'

@Dggz
Copy link

Dggz commented Jul 11, 2024

Is there any chance this will be fixed? Or is there a chance that PRs addressing this will be merged and released? Is this library still maintained at all?

This part of the code is at least part of the issue since True/False are instances of int, that whole conditional block is broken and it works for actual booleans by chance.

https://github.com/h2non/jsonpath-ng/blame/8a62341c1ad94b7c23427a96dd6ea81367d537b7/jsonpath_ng/ext/filter.py#L107

@denisfrm
Copy link

denisfrm commented Jan 8, 2025

Hi guys!

I made an adjustment to this PR #189 by removing the conditional for boolean values ​​because I didn't find any scenario that requires conversion to bool.
It is possible to convert the boolean to int and trap the TypError. It would work. But I really don't see the need for this condition. Like this:

        for data in datum:
            value = data.value
            if type(self.value) is int:
                try:
                    value = int(value)
                except ValueError:
                    continue
            if isinstance(self.value, bool):
                try:
                    value = int(value)
                except (ValueError, TypeError):
                    continue

The int type in Python can be represented by an integer (0, 1), bool is a subclass of int. So to only handle integers, I changed the isinstance condition to type. This way it rejects subclasses.

If anyone finds a scenario that requires conversion to bool, I can add it to the unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants