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

Custom "None" value when search fails #186

Open
fjsj opened this issue Sep 20, 2019 · 10 comments · May be fixed by #311
Open

Custom "None" value when search fails #186

fjsj opened this issue Sep 20, 2019 · 10 comments · May be fixed by #311

Comments

@fjsj
Copy link

fjsj commented Sep 20, 2019

Hi, thanks for this awesome lib!
One question: it seems to be impossible to set a custom return in case the search fails.

E.g.

not_found = object()
jmespath.search('foo.aaa', {'foo': {'bar': 'baz'}})  # I need `not_found`, not `None`

Is this by design or are you open to supporting that?
I see this was discussed at #113
But the proposed solution of contains isn't great, as it requires 2 searches.

@yashpokar
Copy link

You may try

jmespath.search('foo.aaa', {'foo': {'bar': 'baz'}})  or 'not_found'

@fjsj
Copy link
Author

fjsj commented Sep 30, 2019

@yashpokar
Not enough for this problem.
This will make 'not_found' be returned when 'foo.aaa' is None, like here: {'foo': {'aaa': None}}. That's why it's important to differentiate a valid None from a not found value.

@yashpokar
Copy link

@fjsj you're right 👍

@ges0909
Copy link

ges0909 commented Nov 6, 2020

Hi, thanks for this awesome lib!
One question: it seems to be impossible to set a custom return in case the search fails.

E.g.

not_found = object()
jmespath.search('foo.aaa', {'foo': {'bar': 'baz'}})  # I need `not_found`, not `None`

Is this by design or are you open to supporting that?
I see this was discussed at #113
But the proposed solution of contains isn't great, as it requires 2 searches.

Good proposal!!

It has to be differentiated between the legal value 'null' for an existing key and a non-existing key. JMESPath search should return another object than None if a key is not found.

E.g. allow to define an object NotFound as option. If not defined use None for not found. So you are backward compatible.

@cdogaru-hrp-ctr
Copy link

any updates on this?

@OndraSlama
Copy link

Any updates? This seems like quite an important thing to solve imo.

@agargenta
Copy link

+1

1 similar comment
@theobjectivedad
Copy link

+1

@theobjectivedad
Copy link

theobjectivedad commented May 23, 2023

If anyone is looking for a hack, this is what I am using until this issues is resolved. The key_exists function below will check for true existence of key:

def key_exists(key: str, obj: Any, separator='.') -> bool:

    keys = key.split(separator)
    for k in keys:
        if isinstance(obj, dict):
            if k in obj:
                obj = obj[k]
            else:
                return False
        else:
            return False
    return True

To use, check if the search result is None and if the key doesn't exist before implementing handling logic:

result = jmespath.search(search_string, source)

if result is None and not key_exists(search_string, source, separator="."):       
  if match_required:
    # Handle search_string doesn't exist but match is required...

  # Handle search_string doesn't exist and match isn't required, go ahead and assign a default value

# Implied else, search_string was found in source and it may be None

Please use caution, this will easily break when a more sophisticated jmespath expression is used.

@cdogaru-hrp-ctr
Copy link

cdogaru-hrp-ctr commented May 23, 2023

Since jmespath doesn't care about fixing this, we've ended up using jsonpath-ng which supports everything that jmespath supports + handling None values. Best workaround.

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

Successfully merging a pull request may close this issue.

7 participants