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

[feature] JSONPath not supporting multiple conditions? #1116

Closed
ThinkPadNL opened this issue Nov 11, 2022 · 8 comments
Closed

[feature] JSONPath not supporting multiple conditions? #1116

ThinkPadNL opened this issue Nov 11, 2022 · 8 comments
Labels
enhancement New feature or request filters

Comments

@ThinkPadNL
Copy link

Version and OS
v0.39.21.1 on Docker (Linux)

Is your feature request related to a problem? Please describe.
I think this is a bit similar to #623 but then for JSONPath instead of CSS.
I made a filter that works perfectly when testing with https://jsonpath.com/ but changedetection.io filter page is not accepting it 😞 I think it is due to the '&&' operator.

Describe the solution you'd like
Could the feature to use multiple filters (by using '&&' operator) in a JSONPath expression be added?

Describe the use-case and give concrete real-world examples
The filter i made:
$.downloads[?(@.category__slug=='firmware' && !@.name.includes('v1.') && @.name.includes('ER-X-SFP'))].name

When i put this filter into the page, i get the error:
is not a valid JSONPath expression. (Parse error at 1:44 near token & (&))

The same happens with a more simple filter with two conditions, like this one:
$.downloads[?(@.category__slug=='firmware' && @.featured==true)].name
Results in is not a valid JSONPath expression. (Parse error at 1:44 near token & (&))

You can try for yourself, this is the JSON source data: https://pastebin.com/raw/USJSdSWd

Additional context

@dgtlmoon
Copy link
Owner

Ah ok, so current version, I remember we added a fix a while back because someone else reported something similar

@dgtlmoon
Copy link
Owner

Can you try JQ instead? That should work better

@ThinkPadNL
Copy link
Author

JQ is quite a bit more complicated i must say. I have modified my filter and it works good enough for me now. But i think i will need this feature in the future so my question is still relevant (if possible to add the feature ofcourse...).

@dgtlmoon
Copy link
Owner

dgtlmoon commented Nov 12, 2022

yeah hmm, I think it's a discrepency in the library that this project uses to process the jsonpath versus other sites, you know I'm just one guy and didn't write this application and all of the jsonpath handling parser too :-D

@dgtlmoon
Copy link
Owner

dgtlmoon commented Nov 12, 2022

>>> import json
>>> from jsonpath_ng import jsonpath
>>> from jsonpath_ng.ext import parse
>>> jsonpath_expr = parse('$.objects[?(@some_field > 5)]')
>>> 
>>> jsonpath_expr = parse("$.downloads[?(@.category__slug=='firmware' && !@.name.includes('v1.') && @.name.includes('ER-X-SFP'))].name")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dgtlmoon/.local/lib/python3.8/site-packages/jsonpath_ng/ext/parser.py", line 172, in parse
    return ExtentedJsonPathParser(debug=debug).parse(path)
  File "/home/dgtlmoon/.local/lib/python3.8/site-packages/jsonpath_ng/parser.py", line 44, in parse
    return self.parse_token_stream(lexer.tokenize(string))
  File "/home/dgtlmoon/.local/lib/python3.8/site-packages/jsonpath_ng/parser.py", line 67, in parse_token_stream
    return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
  File "/home/dgtlmoon/.local/lib/python3.8/site-packages/ply/yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/home/dgtlmoon/.local/lib/python3.8/site-packages/ply/yacc.py", line 1201, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/home/dgtlmoon/.local/lib/python3.8/site-packages/ply/yacc.py", line 192, in call_errorfunc
    r = errorfunc(token)
  File "/home/dgtlmoon/.local/lib/python3.8/site-packages/jsonpath_ng/parser.py", line 81, in p_error
    raise JsonPathParserError('Parse error at %s:%s near token %s (%s)'
jsonpath_ng.exceptions.JsonPathParserError: Parse error at 1:44 near token & (&)

@dgtlmoon
Copy link
Owner

dgtlmoon commented Nov 12, 2022

@ThinkPadNL
Copy link
Author

ThinkPadNL commented Nov 12, 2022

I managed to rewrite my query into JQ. This is wat i came up with. It retrieves the firmwares for the EdgeRouter X SFP that are published later than 2022-07:

[.downloads[] | select(.category__slug == "firmware") | select(.name | contains("ER-X-SFP")) | select(.date_published > "2022-08") | {name,date_published}]

The expression select(.name | contains("v1") | not) was also handy (as i am on the v2 branch), but i removed it in favour of the date part which works even better.

My question is solved for now. Feel free to close the issue.

@dgtlmoon
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request filters
Projects
None yet
Development

No branches or pull requests

2 participants