1

API URL : https://davids-restaurant.herokuapp.com/menu_items.json?category=C

I'm trying to retrieve name property of ID 913 from the above rest API

Please find my code below

String URL = "https://davids-restaurant.herokuapp.com/menu_items.json?category=C";
Response res = RestAssured.get(URL);
JsonPath jsonPathEvaluator = res.jsonPath();
System.out.println(jsonPathEvaluator.get("$..menu_items[?(@id == 913)].description"));

Error Message

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found '[' @ line 1, column 40.
                            $..menu_items[?(@id == 913)].description


                        ^

I tried this which works but I donot want to query with index but I want to query with ID

System.out.println(jsonPathEvaluator.get("menu_items[1].description"));

1 Answers1

1

You can use Groovy filters to get the result. Have a look at this code.

io.restassured.response Response;
// response= Get the API response
List<String> namesList = from(response.asString()).getList("menu_items.findAll { it.id == 913 }.name");

Here I have created a list but you can find single item by using 'item.find'.

rohit.jaryal
  • 320
  • 1
  • 8