Skip to content

Conversation

memborsky
Copy link

Use re.search() to check if the value being compared is contained within the filter field.

>>> from jsonpath_ng.ext import parse
>>> 
>>> simple_dict = {
...   "phone_numbers": [
...     {
...       "type"  : "iPhone",
...       "number": "0123-4567-8888"
...     },
...     {
...       "type"  : "home",
...       "number": "0123-4567-8910"
...     },
...     {
...       "type"  : "home",
...       "number": "1098-7654-3210"
...     }
...   ]
... }
>>> 
>>> expression = parse('$.phone_numbers[?(@.type =~ "^hom")]')
>>> 
>>> [x.value for x in expression.find(simple_dict)]
[{'type': 'home', 'number': '0123-4567-8910'}, {'type': 'home', 'number': '1098-7654-3210'}]
>>> 
>>> expression = parse('$.phone_numbers[?(@.number =~ "[0-9]{4}-4567-[0-9]{4}")]')
>>> 
>>> [x.value for x in expression.find(simple_dict)]
[{'type': 'iPhone', 'number': '0123-4567-8888'}, {'type': 'home', 'number': '0123-4567-8910'}]

@coveralls
Copy link

Coverage Status

Coverage remained the same at 0.0% when pulling 7ff235a on memborsky:add-regular-expression-contains-support into d4b8b87 on h2non:master.

@h2non h2non merged commit e5405c1 into h2non:master Apr 11, 2020
@memborsky memborsky deleted the add-regular-expression-contains-support branch April 11, 2020 20:53
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 this pull request may close these issues.

3 participants