bignose
bignose

Reputation: 32347

Detect match failure in JMESPath

Using the Python jmespath library, how can I distinguish “matched the expression, the value is None” versus “failed to match the expression”?

The jmespath.search function returns None in two distinct cases:

>>> import jmespath

>>> foo = {'bar': {'lorem': 13, 'ipsum': None}}
>>> repr(jmespath.search('bar.lorem', foo))
'13'
>>> repr(jmespath.search('bar.ipsum', foo))    # Path matches, value None
'None'
>>> repr(jmespath.search('dolor', foo))    # Path does not match
'None'

It appears the JMESPath search API returns None in these two distinct cases. How can the caller know the difference between them?

Upvotes: 1

Views: 343

Answers (1)

bignose
bignose

Reputation: 32347

Currently it appears there is no way to distinguish a match failure versus a success that returns None.

There is an open issue requesting that this should change but for now the answer evidently is “you can't do that with jmespath”.

Upvotes: 1

Related Questions