0

I have a Rest Assured response that contains the following body:

{
   "content":[
      {
         "id":"7db80896",
         "secondaryId":"12F9BD",
         "version":1,
         "details":{
            "status":"VALID",
            "reason":"Passed validations"
         },
         "subscriptionStatuses":[
            {
               "subscriptionId":"b8003508",
               "subscriptionName":"Sub_1",
               "creator":"Person 1",
               "details":{
                  "status":"ACCEPTED",
                  "reason":"Passed validations"
               }
            },
            {
               "subscriptionId":"b8003509",
               "subscriptionName":"Sub_2",
               "creator":"Person 1",
               "details":{
                  "status":"ACCEPTED",
                  "reason":"Passed validations"
               }
            }
         ]
      },
      {
         "id":"7db80895",
         "secondaryId":"11F9BD",
         "version":1,
         "details":{
            "status":"VALID",
            "reason":"Passed validations"
         },
         "subscriptionStatuses":[
            {
               "subscriptionId":"b8003509",
               "subscriptionName":"Sub_2",
               "creator":"Person 2",
               "details":{
                  "status":"ACCEPTED",
                  "reason":"Passed validations"
               }
            }
         ]
      }
   ]
}

I want to verify the status "ACCEPTED" and reason "PASSED VALIDATIONS" which is nested within 2 arrays. I have tried storing the reasons as a list, outlined in this example but I get the following error:

code: var list: List<Any> = response.jsonPath().getList("reason")

error: java.lang.IllegalStateException: response.jsonPath().getList("reason") must not be null

Is there a way to do:

for obj in response:
   for obj2 in obj.subscriptionStatuses:
      assertThat(obj2.status == expected)
      assertThat(obj2.reason == expected)
2
  • Please paste the full json so that others dont have to format it and try it directly. Commented Mar 9, 2020 at 16:26
  • @ArunNair fixed the json, was just missing the first bracket Commented Mar 9, 2020 at 16:35

2 Answers 2

1

There is a way: Deserialize the response into objects. Create classes that match the response.

e.g.

class Status {

  private String id;
  private String secondaryId;
  //etc.
  private StatusDetail details;
  private List<SubscriptionStatus> subscriptionStatuses;
}

...and so on. I don't know what this would look like in Kotlin specifically, but it would be similar.

Notice that the nested objects in the JSON response are their own classes, and the field names in each class should match the field names in your JSON.

Then, you can use ObjectMapper to deserialize. You may need to do response.jsonPath().getList("$.content") first, but I don't know what your response object is to provide more guidance than that. Preferably, you'd just get the JSON string directly:

List<Status> statuses = new ObjectMapper().readValue(jsonString, 
  new TypeReference<List<Status>>() {})

Note that you can re-use ObjectMapper and shouldn't create a new one every time, and there are options for ObjectMapper as well to allow null values.

Sign up to request clarification or add additional context in comments.

1 Comment

I should have said, "is there an easy, one liner way to do it?" Thanks for this though. It's very helpful, just also long winded.
0

You can get it using the following code

List<String> m1 =response.jsonPath().get("content.subscriptionStatuses.details");
System.out.println(m1);

Output will show [[{status=ACCEPTED, reason=Passed validations}, {status=ACCEPTED, reason=Passed validations}], [{status=ACCEPTED, reason=Passed validations}]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.