Search code examples
jsonparsinggroovyjsonpathjsonparser

how to escape special character colon (':') in jsonpath


Json:

{
    "im:rating": {
        "label": "1"
    }
}

While trying to use Jsonpath in groovy If i am using this '$.im:rating'

It is showing following error

Caused by: com.nebhale.jsonpath.InvalidJsonPathExpressionException: Illegal

character 'PathCharacter [types=[], value=:, position=4]' $.im:rating ----^ Illegal character 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=r, position=5]' $.im:rating -----^ Illegal character 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=a, position=6]' $.im:rating ------^ Illegal character 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=t, position=7]' $.im:rating -------^ Illegal character 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=i, position=8]' $.im:rating --------^ Illegal character 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=n, position=9]' $.im:rating ---------^ Illegal character 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=g, position=10]' $.im:rating ----------^

  at com.nebhale.jsonpath.JsonPath.compile(JsonPath.java:85)
  at com.nebhale.jsonpath.JsonPath.read(JsonPath.java:182)

Solution

  • We can access alternatively with the following

    '$..label'