1

I am making use of non-static import

JsonPath jp = response.jsonPath();
System.out.println(jp.get("data?(@.id>14).employee_name").toString());

For a JSON as shown below:

{"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""},{"id":"2","employee_name":"Garrett Winters","employee_salary":"170750","employee_age":"63","profile_image":""}]}

When i am trying to run it , i am getting below error:

    java.lang.IllegalArgumentException: Invalid JSON expression:
    Script1.groovy: 1: expecting EOF, found '[' @ line 1, column 31.
                                data[?(@.id>14)].employee_name
                                 ^

1 error

Can someone guide me why is this error being thrown ?

user9261795
  • 363
  • 1
  • 2
  • 8

1 Answers1

0

I doubt if that syntax is right, nonetheless, you should be using the below

Also note that the id is a string in your response so you will have to include it in quotes

js.get("data.find {it.id > '14'}.employee_name").toString();
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
  • With this syntax, i am getting the below error :java.lang.IllegalArgumentException: The parameter "it" was used but not defined. Define parameters using the JsonPath.params(...) function – user9261795 Jul 07 '20 at 03:07
  • Are you sure ? Cause this is a working example for the same json you've posted, can you post your complete code ? – Wilfred Clement Jul 07 '20 at 03:58
  • Here it is : Response resp = RestAssured.get("http://dummy.restapiexample.com/api/v1/employees"); System.out.println(resp.getStatusCode()); Assert.assertEquals(resp.getStatusCode(), 200); JsonPath jp = resp.jsonPath(); System.out.println(jp.get("data.employee_name").toString()); System.out.println(jp.get("data.id").toString()); System.out.println(jp.get("data.find(it.id > '14').employee_name").toString()); – user9261795 Jul 07 '20 at 05:38
  • There's a mistake in your code, Its supposed to be `{it.id > '14'}` and not `(it.id > '14')` – Wilfred Clement Jul 07 '20 at 05:47