Reputation: 1
I am using jsonpath_ng library to build a jsonpath expression.But getting error while parsing expression as expression contains /. jsonpath_ng.lexer.JsonPathLexerError: Error on line 1, col 8: Unexpected character: /
Tried jsonpath_rw_ext,jsonpath_ng.ext libraries ,still facing similar errors.
from jsonpath_ng import jsonpath,parse
jsonpath_expression = parse('$.paths./createJob.post.parameters[0].schema.$ref') ```
Upvotes: 0
Views: 1966
Reputation: 64959
The error is arising because the jsonpath_ng library appears to interpret the /
symbol as numeric division, not as a valid character in identifiers.
At the same time, however, it's unusual (though not impossible) for the JSON to have /
characters in property names.
As I see it, what you need to do depends on the data you have:
If your JSON genuinely has a /
character in property names, i.e. it looks like the following:
{"paths": {"/createJob": {"post": ... } } }
then you'll need to use the bracket notation (['propertyname']
) instead of the dot notation (.propertyname
):
jsonpath_expression = parse("$.paths['/createJob'].post.parameters[0].schema.$ref")
If your JSON doesn't actually have /
characters in property names, then you should just delete the /
from your JSON Path expression.
Note that I cannot guarantee either of these approaches to return data, because you haven't provided any sample data to query nor the expected result of running the JSON Path query.
Upvotes: 2