0

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') ```

Priyanka
  • 1
  • 3

1 Answers1

1

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:

  1. 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")
    
  2. 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.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • Hi @Luke Woodward , My Json have genuinely has / characters in property name.Using (['propertyname']) did solved my problem. Thanks a lot. – Priyanka May 26 '20 at 09:33