0

We have REST API automation scripts using RestAssured. In this declared response object as public static Response response; and retrieving the response data using response.jsonPath().get("id"), during this trying to even get the size or length of the id, even need to get details about tags array.

JSON Response:

   [
  {
    "id": 1,
    "name": "test1",
    "tags": [
      {
        "tagType": "details1",
        "tag": {
          "description": null
        }
      }
    ]
  },
  {
    "id": 2,
    "name": "test2",
    "tags": [
      {
        "tagType": "details2",
        "tag": {
          "description": null
        }
      }
    ]
  }
]

Tried below ways:

public static Response response;

List<String> resIDs = response.jsonPath().get("id");

System.err.println("Retrieved IDs from Response: " + resIDs);

O/P: is [1,2,3,4,5,6,7]

Tried as resIDs.size(), that also no response printed.

List<Object> size = response.jsonPath().getList("$");

System.err.println("ArraySize for IDs from Response: " + size);

or

int size = response.jsonPath().getList("$").size();

O/P: Not printed/nothing shown

Please guide how to get the size/length.

la1
  • 519
  • 1
  • 8
  • 30
  • 1
    so you just need the output as 7 ? – Wilfred Clement Jun 26 '20 at 09:59
  • Thanks for the reply and extending help, yes, also wants to get size details of "tags" which is nested array. – la1 Jun 26 '20 at 10:44
  • 1
    `resIDs.size()` returns a proper count for me – Wilfred Clement Jun 26 '20 at 10:51
  • I am still unable to get, is this the way tried: List resIDs = response.jsonPath().get("id"); System.err.println("ArraySize for IDs from Response: " + resIDs.size()); – la1 Jun 26 '20 at 16:25
  • 1
    That's exactly how its supposed to be, Look at the output of mine [here](https://ibb.co/NKWGHLB) – Wilfred Clement Jun 26 '20 at 16:37
  • Thanks for quick response. I have below code for response: response = RESTServiceBase.postCallWithJsonBodyParam(jsonObject.toString(), url_endPoint); the Method postCallWithJsonBodyParam: public static Response postCallWithJsonBodyParam(String jsonObject, String URL) { LoggerUtil.log("POST call on endpoint " + URL + " with JSON String Parameter " + jsonObject); return given().relaxedHTTPSValidation().contentType(getContentType()).request().body(jsonObject).when().post(URL); }; And with the above response constructing: List resIDs = response.jsonPath().get("id"); – la1 Jun 26 '20 at 16:44

1 Answers1

1

I don't seem to find any issue in your code, I just changed a bit to run locally and its working fine. Here's my code

public class S_62591968 {

    public static Response postCallWithJsonBodyParam(String URL) {
        return RestAssured.given().relaxedHTTPSValidation().contentType(ContentType.JSON).request().when().get(URL);
    }

    public static void main(String[] args) {

        String url_endPoint = "http://localhost:8089/def/abc";
        Response response = postCallWithJsonBodyParam(url_endPoint);

        List<String> resIDs = response.jsonPath().get("id");
        System.out.println("Retrieved IDs from Response : " + resIDs);
        System.out.println("ArraySize for IDs from Response : " + resIDs.size());
    }
}

Console :

Retrieved IDs from Response : [1, 2]
ArraySize for IDs from Response : 2
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29