Harish February 2016

How to fetch JSON object from Json array in REstAssured

Can anyone please help me to solve this scenario:

I am new to RestAssured and handling JSON in our automation script. I have an API whose response is JSONArray i.e.,

  [{
    "id": 1002,
    "entity": "testcase",
    "fieldName": "TextName",
    "displayName": "Name"
  }, {
    "id": 1003,
    "entity": "testcase",
    "fieldName": "steps",
    "displayName": "TestSteps"
  }]

While automation, for verification i need to fetch the reponse. I have tried the below one but not getting expected output

 String API = "/field/entity/testcase"
 Response response = given().auth().preemptive().basic("test.manager",     "test.manager").when().get(API);
    JSONObject JSONResponseBody = new   JSONObject(response.body().asString());
    Assert.assertEquals(JSONResponseBody.getString("fieldName"), "TextName");

and also i tried with this:

    JSONArray array = new JSONArray();
    JsonObject JSONResponseBody = array.getJsonObject(0);

Thanks Inadvance

Answers


Kruti Patel February 2016

You should try this:

String API = "/field/entity/testcase"
Response response = given().auth().preemptive().basic("test.manager", "test.manager").when().get(API);
JSONArray JSONResponseBody = new   JSONArray(response.body().asString());
Assert.assertEquals(JSONResponseBody.getJsonObject(0).getString("fieldName"), "TextName");

Post Status

Asked in February 2016
Viewed 2,315 times
Voted 12
Answered 1 times

Search




Leave an answer