#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Rest Assured

Extracting Response Body data From Response Using Restassured

Extracting the Full Body

String body = given()
    .when().get("/users/2")
    .then().extract().asString();

Extracting a Response Object

Response response = given().when().get("/users/2");
String name = response.jsonPath().getString("data.first_name");

Why This Matters

Extracting the body lets a test store, log, or pass response data into later steps — for example, capturing a newly created resource's ID to use in a follow-up request.

Other Ways to Pull Values Out of a Response

Rest Assured gives you several extraction styles. Picking the right one keeps tests short and readable. Assume the API returns this JSON:

{
  "data": {
    "id": 2,
    "email": "janet.weaver@example.com",
    "first_name": "Janet",
    "roles": ["viewer", "editor"]
  }
}

Each of these lines reads from the same body:

// 1. Single value straight from the chain
int id = given().when().get("/users/2")
        .then().statusCode(200)
        .extract().path("data.id");

// 2. A list of values
List<String> roles = response.jsonPath().getList("data.roles");

// 3. Status, headers and timing from the same Response
int status      = response.getStatusCode();
String type     = response.getHeader("Content-Type");
long millis     = response.getTime();

// 4. Map the JSON onto a Java class
UserWrapper user = response.as(UserWrapper.class);
System.out.println(user.getData().getEmail());

Option 4, called deserialization, needs Jackson or Gson on the classpath. It is the cleanest choice when a test checks many fields, because your IDE then offers autocomplete for every property.

A simple rule of thumb helps you choose. Use extract().path() when you need one value for the next step. Use jsonPath() when you need several values from a stored response. Use a POJO when the response is large or reused across many tests, since a change in the API then shows up as a single compile error rather than many broken string paths.

Chaining Requests with Extracted Data

Real API flows often depend on the previous response. A typical create-then-fetch test works in four steps:

  1. Send a POST that creates a resource.
  2. Assert the status code before extracting anything.
  3. Extract the new ID from the body.
  4. Use that ID as a path parameter in the next request.
String newId = given()
        .contentType(ContentType.JSON)
        .body("{\"name\":\"Asha\",\"job\":\"tester\"}")
    .when()
        .post("/users")
    .then()
        .statusCode(201)
        .extract().path("id");

given()
    .pathParam("id", newId)
.when()
    .get("/users/{id}")
.then()
    .statusCode(200);

Putting statusCode() before extract() matters. If the POST fails, the test stops with a clear status error instead of a confusing null pointer later on.

Filtering Arrays with GPath

Rest Assured paths use Groovy's GPath syntax, which can search inside arrays without loops. For a list endpoint returning many users:

// first user whose last name is "Lawson"
String email = response.jsonPath()
        .getString("data.find { it.last_name == 'Lawson' }.email");

// all ids greater than 8
List<Integer> ids = response.jsonPath()
        .getList("data.findAll { it.id > 8 }.id");

For XML responses, the same idea works through response.xmlPath().

Common Extraction Mistakes

  • Wrong type: asking for a String when the JSON holds a number causes a cast error. Use getInt or getString deliberately.
  • Extracting instead of asserting: if you only need to check a value, body("data.id", equalTo(2)) is shorter and gives better failure messages.
  • Calling the API twice: store the Response once and read from it, rather than sending the same request again for each value.
  • Logging secrets: avoid printing full bodies that contain tokens; use log().ifValidationFails() for debugging instead.
  • Brittle paths: deeply nested string paths copied from one response break when the API adds a wrapper object. Keep paths in one helper or use POJOs.

Checklist: validate status first, extract once, use typed getters or POJOs, and pass only the values the next step needs.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course