Python JSONPath filter expression error, jsonpath-rw 1.4.0 unexpected character … here is a solution to the problem.
Python JSONPath filter expression error, jsonpath-rw 1.4.0 unexpected character
I installed jsonpath-rw 1.4.0
sudo apt-get install python-jsonpath-rw
It comes with a /usr/bin/jsonpath
. But this gives me an error when using filter expressions:
$ jsonpath '$.. book[?( @.price < 10)]' book.json
Traceback (most recent call last):
File "/usr/bin/jsonpath", line 11, in <module>
load_entry_point('jsonpath-rw==1.4.0', 'console_scripts', 'jsonpath.py')()
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/bin/jsonpath.py", line 71, in entry_point
main(*sys.argv)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/bin/jsonpath.py", line 57, in main
expr = parse(args.expression)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/parser.py", line 14, in parse
return JsonPathParser().parse(string)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/parser.py", line 32, in parse
return self.parse_token_stream(lexer.tokenize(string))
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/parser.py", line 55, in parse_token_stream
return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
File "/usr/lib/python2.7/dist-packages/ply/yacc.py", line 331, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "/usr/lib/python2.7/dist-packages/ply/yacc.py", line 1061, in parseopt_notrack
lookahead = get_token() # Get the next token
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/parser.py", line 179, in token
return next(self.iterator)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/lexer.py", line 33, in tokenize
t = new_lexer.token()
File "/usr/lib/python2.7/dist-packages/ply/lex.py", line 393, in token
newtok = self.lexerrorf(tok)
File "/usr/lib/python2.7/dist-packages/jsonpath_rw/lexer.py", line 165, in t_error
raise JsonPathLexerError('Error on line %s, col %s: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
jsonpath_rw.lexer.JsonPathLexerError: Error on line 1, col 8: Unexpected character: ?
So does jsonpath-rw 1.4.0 not support Filter expressions?
It seems to be working fine according to this blog post , But that was in 2015.
Has the situation changed since then?
Solution
I made it work by using jsonpath-rw-ext instead of jsonpath-rw
. This seems to be the one that supports filter extensions.