2

I am writing an integration test to my microservice using rest assured. I have a Json payload like this which is returned from it.

{
   "sessionQuestions":[
      {
         "id":1272,
         "sessionId":1146,
         "questionId":"1002",
      },
      {
         "id":1273,
         "sessionId":1146,
         "questionId":"1004",
      }
   ]
}

I need to find the id of a sessionquestion given the questionId value. Then I need to compare them in my assertion. I am having hard time fetching the id off of a sessionQuestion given the questionId value. I am using JsonPath to get this thing done. And here's my Json path expression.

new JsonPath(response).get("$.sessionQuestions[?(@.questionId=='1002')].id");

This throws an error.

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found '[' @ line 1, column 45.
   nRootObject.$.sessionQuestions[?(@.quest
                                 ^

1 error

   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

I tried the same Json path expression in this online evaluator, which works fine. I am using json-path-2.9.0.jar

What I am doing wrong here? Any help is really appreciated.

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63

1 Answers1

5

Which version of JsonPath do you use? I use this.

And the code which works for your case is:

JsonPath.parse(response).read("$.sessionQuestions[?(@.questionId=='1002')].id").toString();

And try with this JSON:

{
   "sessionQuestions":[
      {
         "id":1272,
         "sessionId":1146,
         "questionId":"1002"
      },
      {
         "id":1273,
         "sessionId":1146,
         "questionId":"1004"
      }
   ]
}

Edit: If you are using this dependancy (json-path-2.9.0), you have to do it like this:

from(response).getList("sessionQuestions.findAll { it.questionId=='1002' }.id").toString();

and you have to import from like this:

import static com.jayway.restassured.path.json.JsonPath.from;
ddarellis
  • 3,912
  • 3
  • 25
  • 53
  • No luck. Updated the question with the Json path version which I am using. Thanks. – Ravindra Ranwala Dec 08 '17 at 13:03
  • Unfortunately it does not have the `parse` and `read` methods. – Ravindra Ranwala Dec 08 '17 at 13:07
  • I updated my answer. Try to be more specific to your upcoming posts :) Also take a look here https://github.com/rest-assured/rest-assured/wiki/Usage – ddarellis Dec 08 '17 at 13:25
  • 1
    Thanks for the solution. `from(response).getList("sessionQuestions.findAll { it.questionId=='1002' }.id").toString();` helped me when I'm directly using rest-assured's jsonpath. Jayway's jsonpath is different than the rest-assured one's, hence it leads to more complexity. – CMM Jul 28 '20 at 22:43
  • @CMM this is very helpful reply from you, i am usign JsonPath for validating the resposen, thanks for mentioning that this trick works directely with jsonPath as below ==========================>JsonPath pastPartnerUsersListResponseJson= pastPartnerUsersListResponse.getBody().jsonPath(); String pendingCcoId= pastPartnerUsersListResponseJson.getList("data.findAll{it.accessOrder==1}.ccoId").stream().collect(Collectors.toList()).get(0).toString(); I have one basic question here, what is that it, you used , how is it used – Rajaji Aug 13 '20 at 06:49
  • Later to this party, Rest-Assured JsonPath is actually Groovy Path... – Jerry Chin Apr 10 '23 at 02:19