Rest-assured. Is it possible to extract value from request json?

JavaJsonRestRest Assured

Java Problem Overview


I'm getting response this way:

Response response = expect().statusCode(200).given().body(requestBody).contentType("application/json")
.when().post("/admin");
String responseBody = response.getBody().asString();

I have a json in responseBody:

{"user_id":39}

Could I extract to string using rest-assured's method only this value = 39?

Java Solutions


Solution 1 - Java

You can also do like this if you're only interested in extracting the "user_id":

String userId = 
given().
        contentType("application/json").
        body(requestBody).
when().
        post("/admin").
then().
        statusCode(200).
extract().
        path("user_id");

In its simplest form it looks like this:

String userId = get("/person").path("person.userId");

Solution 2 - Java

I found the answer :)

Use JsonPath or XmlPath (in case you have XML) to get data from the response body.

In my case:

JsonPath jsonPath = new JsonPath(responseBody);
int user_id = jsonPath.getInt("user_id");

Solution 3 - Java

There are several ways. I personally use the following ones:

extracting single value:

String user_Id =
given().
when().
then().
extract().
        path("user_id");

work with the entire response when you need more than one:

Response response =
given().
when().
then().
extract().
        response();

String userId = response.path("user_id");

extract one using the JsonPath to get the right type:

long userId =
given().
when().
then().
extract().
        jsonPath().getLong("user_id");

Last one is really useful when you want to match against the value and the type i.e.

assertThat(
    when().
    then().
    extract().
            jsonPath().getLong("user_id"), equalTo(USER_ID)
);

The rest-assured documentation is quite descriptive and full. There are many ways to achieve what you are asking: https://github.com/jayway/rest-assured/wiki/Usage

Solution 4 - Java

To serialize the response into a class, define the target class

public class Result {
    public Long user_id;
}

And map response to it:

Response response = given().body(requestBody).when().post("/admin");
Result result = response.as(Result.class);

You must have Jackson or Gson in the classpath as stated in the documentation.

Solution 5 - Java

you can directly use the response object as well.

Response response = expect().statusCode(200).given().body(requestBody).contentType("application/json").when().post("/admin");

String userId = response.path("user_id").toString();

Solution 6 - Java

JsonPath jsonPathEvaluator = response.jsonPath();
return jsonPathEvaluator.get("user_id").toString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayView Question on Stackoverflow
Solution 1 - JavaJohanView Answer on Stackoverflow
Solution 2 - JavaJayView Answer on Stackoverflow
Solution 3 - JavamagiccrafterView Answer on Stackoverflow
Solution 4 - JavaMilankaView Answer on Stackoverflow
Solution 5 - JavaSabari SriView Answer on Stackoverflow
Solution 6 - JavaPTTView Answer on Stackoverflow