jsongroovyrest-assuredgpathrest-assured-jsonpath

Escape dots in Groovy GPath


I am using the RestAssured framework in Java, whose documentation contains this note

Note that the "json path" syntax uses Groovy's GPath notation and is not to be confused with Jayway's JsonPath syntax.

I need to validate the following JSON:

"_source": {
    "logSource": {
      "logger.name": "LogbackLogger",
    },
}

And the selectors like
_source.logSource.logger.name or
_source.logSource.logger.name[0] return no result.

I assume this is due to the dot in the logger.name property. If no escaping is done, logger.name is interpreted as if name was under the logger, which is not true.

How do I correctly escape the dot character in the GPath, so that logger.name is considered as a single property name?

Thanks!


Solution

  • You have a trivial issue.

    Just wrap it in between single quote i.e., 'logger.name' as there is special character.

    Here is complete example :

    def string = """{  "_source": {
           "logSource": {
              "logger.name": "LogbackLogger",
          }
        }
    }"""
    
    def json = new groovy.json.JsonSlurper().parseText(string)
    println json.'_source'.logSource.'logger.name'