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

Issue with "contains" filter expression #67

Open
jmrMachado opened this issue Mar 6, 2023 · 1 comment
Open

Issue with "contains" filter expression #67

jmrMachado opened this issue Mar 6, 2023 · 1 comment

Comments

@jmrMachado
Copy link

jmrMachado commented Mar 6, 2023

jsonpath2 version

0.4.5

Platform Details

MacOS Ventura, Macbook M1 2020

Scenario:

I'm trying to use the "contains" filter expression but I'm not able to work with it. Also wanted to use it to check if an array contains a specific value, like this:
'$[?(["FR", "DE"] contains @.country_code)].elfCode'
But it also doesn't work

Steps to Reproduce:

Here's the code:
`from jsonpath2.path import Path

sample = {"country_code": "FR", "elfCode": "DE"}
expression = '$[?(@.country_code contains "FR")].elfCode'
p = Path.parse_str(expression)
[m.current_value for m in p.match(sample)]`

Expected Result:

["DE"]

Actual Result:

`InputMismatchException Traceback (most recent call last)
File ~/.pyenv/versions/3.8.11/envs/arttencecommons/lib/python3.8/site-packages/jsonpath2/parser/JSONPathParser.py:801, in JSONPathParser.subscriptable(self)
800 self.state = 111
--> 801 self.match(JSONPathParser.PAREN_RIGHT)
802 pass

File ~/.pyenv/versions/3.8.11/envs/arttencecommons/lib/python3.8/site-packages/antlr4/Parser.py:126, in Parser.match(self, ttype)
125 else:
--> 126 t = self._errHandler.recoverInline(self)
127 if self.buildParseTrees and t.tokenIndex==-1:
128 # we must have conjured up a new token during single token insertion
129 # if it's not the current symbol

File ~/.pyenv/versions/3.8.11/envs/arttencecommons/lib/python3.8/site-packages/antlr4/error/ErrorStrategy.py:407, in DefaultErrorStrategy.recoverInline(self, recognizer)
406 # even that didn't work; must throw the exception
--> 407 raise InputMismatchException(recognizer)

InputMismatchException: None

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
Cell In[31], line 5
3 sample = {"country_code": "FR", "elfCode": "DE"}
...
File ~/.pyenv/versions/3.8.11/envs/arttencecommons/lib/python3.8/site-packages/jsonpath2/parser/init.py:70, in _ConsoleErrorListener.syntaxError(self, recognizer, offendingSymbol, line, column, msg, e)
69 def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
---> 70 raise ValueError("line {}:{} {}".format(line, column, msg))

ValueError: line 1:19 mismatched input 'contains' expecting ')'`

Screenshot 2023-03-06 at 11 05 48

@rupeshkumarmall
Copy link

rupeshkumarmall commented Feb 6, 2024

I am getting the same issue with the following expression and the input json data,

json_data = { "person": { "name": "John", "details": { "age": 25, "address": { "city": "New York", "country": "USA", "locale": ['en_US', 'en_IN'] } } } }

jsonpath2_with_filter_expression_4 = '$["person"]["details"]["address"][?(@.locale contains "en_IN")]' parsed_jsonpath2_path = Path.parse_str(jsonpath2_with_filter_expression_4) match_jsonpath2 = parsed_jsonpath2_path.match(json_data) for match_data in match_jsonpath2: print(f"Value: {match_data.current_value}") print(f"JSONPath: {match_data.node.tojsonpath()}")

Error:- line 1:45 mismatched input 'contains' expecting ')'
antlr4.error.Errors.InputMismatchException: None

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

No branches or pull requests

2 participants