Skip to content

Using a filter in a count() function results in 'unbalanced parentheses' error #13

@MaximeDeforche

Description

@MaximeDeforche

I seem to be running into an issue when trying to use a filter inside the count() function.

For example, the following JSONPath query:
$[? count(@.likes[? @.location]) > 3]
results in an unbalanced parentheses error even when the parentheses seem to fine.

The error can be reconstructed using the above JSONPath query alongside the following JSON document: twitter.json. The stacktrace of the error is provided below.

Thank you for looking into this.

JSONPathSyntaxError                       Traceback (most recent call last)
----> 1 res = jsonpath.find('$[? count(@.likes[? @.location]) > 3]', doc.doc).values()

File /usr/local/lib/python3.13/site-packages/jsonpath_rfc9535/environment.py:145, in JSONPathEnvironment.find(self, query, value)
    126 def find(
    127     self,
    128     query: str,
    129     value: JSONValue,
    130 ) -> JSONPathNodeList:
    131     """Apply the JSONPath expression _query_ to JSON-like data _value_.
    132 
    133     Arguments:
   (...)    143             an incompatible way.
    144     """
--> 145     return self.compile(query).find(value)

File /usr/local/lib/python3.13/site-packages/jsonpath_rfc9535/environment.py:101, in JSONPathEnvironment.compile(self, query)
     87 def compile(self, query: str) -> JSONPathQuery:  # noqa: A003
     88     """Prepare a JSONPath expression ready for repeated application.
     89 
     90     Arguments:
   (...)     99             unacceptable type.
    100     """
--> 101     tokens = tokenize(query)
    102     stream = TokenStream(tokens)
    103     return JSONPathQuery(env=self, segments=tuple(self.parser.parse(stream)))

File /usr/local/lib/python3.13/site-packages/jsonpath_rfc9535/lex.py:490, in tokenize(query)
    487 lexer.run()
    489 if tokens and tokens[-1].type_ == TokenType.ERROR:
--> 490     raise JSONPathSyntaxError(tokens[-1].message, token=tokens[-1])
    492 return tokens

JSONPathSyntaxError: unbalanced parentheses, line 1, column 30

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions