Search code examples
jsonapirestrest-assuredrest-assured-jsonpath

Invalid JSON Expression


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 ?


Solution

  • 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();