58

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?

Jay
  • 1,389
  • 1
  • 10
  • 15
  • Try looking for info on how to parse JSON in Java -- to convert the JSON (in your case) into a Map. You will, unfortunately, find about 20 different ways to do it, most of which are too complicated, but the Java gurus seem to like it that way. – Hot Licks Jan 16 '14 at 18:24
  • Thanks, @HotLicks, I knew this decision, I was looking for answer with rest-assured only. It looks like it couldn't do it. – Jay Jan 17 '14 at 08:26
  • @Jay this is an old question but it seems to me that it is miss-labeled. your title is "extract value from request json" , shouldn't it be "extract label from response json"? All the answers below assume you meant response ... – mancocapac Jul 05 '18 at 18:18

6 Answers6

50

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");
Johan
  • 37,479
  • 32
  • 149
  • 237
31

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");
Jay
  • 1,389
  • 1
  • 10
  • 15
26

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

magiccrafter
  • 5,175
  • 1
  • 56
  • 50
13

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.

Milanka
  • 1,742
  • 19
  • 15
  • This classic DTO approach is fine for small static responses. But the overhead of maintaining all that boilerplate is a drag for ad-hoc/dynamic/large responses. – MarkHu May 16 '21 at 00:03
1

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();
Sabari Sri
  • 33
  • 6
-2
JsonPath jsonPathEvaluator = response.jsonPath();
return jsonPathEvaluator.get("user_id").toString();
PTT
  • 526
  • 7
  • 27