2

I got the response from REST API into Response object after called through RestAssured API.

Response body is json, i want to get the particular key value from that?

Following is the code

Response res = given()
             .relaxedHTTPSValidation()
             .with()
             .contentType(ConfigReader.get("application.json")) 
             .then()
             .get(url);

String rbody = res.body().asString();

How to get particular key value in rbody string?

Sarada Akurathi
  • 1,164
  • 7
  • 28
  • 56

4 Answers4

4

Response class has method path() using that, user can give the json path to get the particular value.

Eg:-

Response res = given()
             .relaxedHTTPSValidation()
             .with()
             .contentType(ConfigReader.get("application.json")) 
             .then()
             .get(url);
String value = res.path("root.childKey").toString();

root.childKey is the path of the json element

Sarada Akurathi
  • 1,164
  • 7
  • 28
  • 56
3

The JasonPath class that is a part of Restassured is the one that I used for my project. First you need to import the JsonPath class using:

import com.jayway.restassured.path.json.JsonPath;

Then you need to pass the JSON string and use it to create the JsonPath object. From the JsonPath object you can use the key to get the corresponding value. The following code will work for you.

Response res = given()
         .relaxedHTTPSValidation()
         .with()
         .contentType(ConfigReader.get("application.json")) 
         .then()
         .get(url);

String rbody = res.asString();
JsonPath jp = new JsonPath( rbody );
String value = jp.getString( "your.key" );
1

JSON is formated like this {someProprty:"someValue"} so instead of getting it as a String, you'll want to access that specific property. ie: b.body.someProperty

Note: I strongly encourage you to name your response something more like res or response. You aren't going to enjoy having b as your response.

How to access JSON Object name/value?

JSON can also be formatted like {somePropertyThatIsNumerical:1} or can contain arrays.

Community
  • 1
  • 1
Glen Pierce
  • 4,401
  • 5
  • 31
  • 50
  • Hi Glen, thanks for answer and suggestion. I didn't get any method to directly give the json key as `res.body.someProperty`. i checked it has `body() or getbody()` methods and then to directly access the key, i didn't find any method in `Response` class. – Sarada Akurathi May 02 '17 at 05:36
  • Hi Glen, i found the answer, we can use `res.path()` function eg:- `b.path("config")`, Thanks – Sarada Akurathi May 02 '17 at 07:53
0
baseURI="url";
        Map<String,String> reqParam=new HashMap<String,String>();
        reqParam.put("loginID","abc");
        reqParam.put("password","123");

        JSONObject reqObjects=new JSONObject(reqParam);
        Response response = 
        given()
          .header("Content-Type", "application/json").accept(ContentType.JSON)
          .when()
          .body(reqObjects.toJSONString()).post("/v1/getDetails")
          .then().log().body().extract().response();

         String responseBody= response.asString();
         JsonPath path=new JsonPath(responseBody);
         String key=path.getString("path.key");