0

I am working with RestAssured framework in my java project but i got an error from Groovy . I need to find a bunch of elements in my json. My json looks like as below:

[ {
  "city" : "LONDON",
  "car" : "myCar",
  "name" : "Jack",
  "familyName" : "Joe"
} ]

Here is how i make the keys to search on json:

  List<String> gfg = new ArrayList<>(List.of("$.[0].city", "$.[0].car", "$.[0].name", "$.[0].familyName")); 

  for (String path : jsonPaths) {
      String key = "\"" + path + "\"";
      Assert.assertTrue(rawRes.jsonPath().getString(key) != null);
    }

Here the rawRes is the Response type from RestAssured framework which contains the json in its body.

But then in the line rawRes.jsonPath().getString(key) it complains with:

Script1.groovy: 1: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 1, column 29.
   restAssuredJsonRootObject."$.[0].endTimeLocal"
Jeff
  • 7,767
  • 28
  • 85
  • 138
  • 1
    Double-quotes is for a "Groovy string" where `$` has meaning. So I would try single-quotes such as this: `def gfg = ['$.[0].city', '$.[0].car', '$.[0].name', '$.[0].familyName']` – Michael Easter Oct 30 '20 at 11:03
  • @Jeff, is it a java code or groovy: `List gfg = ...` ? why do you have `$` in your json paths? i don't see it's supported in restassured... – daggett Oct 31 '20 at 15:04
  • according to doc: [JsonPath](https://www.javadoc.io/doc/io.rest-assured/json-path/latest/io/restassured/path/json/JsonPath.html) is an alternative to using XPath for easily getting values from a Object document. It follows the Groovy [GPath](http://docs.groovy-lang.org/latest/html/documentation/#_gpath) syntax when getting an object from the document. – daggett Oct 31 '20 at 15:07

0 Answers0