-
Notifications
You must be signed in to change notification settings - Fork 181
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
Comments
You may try jmespath.search('foo.aaa', {'foo': {'bar': 'baz'}}) or 'not_found' |
@yashpokar |
@fjsj you're right 👍 |
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. |
any updates on this? |
Any updates? This seems like quite an important thing to solve imo. |
+1 |
1 similar comment
+1 |
If anyone is looking for a hack, this is what I am using until this issues is resolved. The 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. |
Since |
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.
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.The text was updated successfully, but these errors were encountered: