#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 Json Field Data From Response Using Restassured

Direct Assertions

.then().body("data.email", equalTo("janet.weaver@reqres.in"));

Extracting Into a Variable

String email = given()
    .when().get("/users/2")
    .then().extract().path("data.email");

Working With Arrays

List<Integer> ids = response.jsonPath().getList("data.id");

Why This Matters

Extracting specific fields is essential both for detailed assertions and for chaining values (like an ID or token) into subsequent requests.

Understanding GPath Expressions

Rest Assured reads JSON using GPath, a Groovy-based path syntax. Each dot moves one level deeper into the JSON tree, and square brackets pick an item from an array.

{
  "page": 1,
  "data": [
    { "id": 7, "email": "michael@reqres.in", "first_name": "Michael" },
    { "id": 8, "email": "lindsay@reqres.in", "first_name": "Lindsay" }
  ],
  "support": { "url": "https://reqres.in/#support" }
}
PathReturns
page1
data[0].emailThe first user's email
data.first_nameA list of all first names
data.size()Number of users in the array
data.find { it.id == 8 }.emailEmail of the user whose id is 8
data.findAll { it.id > 7 }.first_nameNames of users with id above 7
support.urlA nested object field

A Worked Example: Create, Extract, Reuse

A very common pattern is to create a record, pull its new ID from the response, and use that ID in the next call. Extract the Response once and read as many fields as you need from it.

Response created = given()
        .contentType(ContentType.JSON)
        .body("{\"name\":\"Asha\",\"job\":\"QA\"}")
    .when()
        .post("/api/users")
    .then()
        .statusCode(201)
        .extract().response();

String userId = created.jsonPath().getString("id");
String name   = created.jsonPath().getString("name");

given()
    .pathParam("id", userId)
.when()
    .get("/api/users/{id}")
.then()
    .statusCode(anyOf(is(200), is(404)));

For large responses you can map the JSON straight into a Java class with response.as(User.class) or jsonPath().getObject("data[0]", User.class). That needs Jackson or Gson on the classpath and gives you typed getters instead of string paths.

Picking the Right Extraction Style

  • extract().path() is quick for one value.
  • jsonPath() is better when you need several fields or typed getters such as getInt and getList.
  • POJO mapping suits complex, stable response shapes.
  • Direct body() assertions are clearest when you only want to check a value, not reuse it.

Common Mistakes

  • Calling the endpoint twice, once to assert and once to extract. Extract the response once instead.
  • Reading a number as a String and then comparing it wrongly; use getInt or getLong.
  • Forgetting that a path into an array returns a list, not a single value.
  • Using a path that does not exist; GPath returns null rather than failing, so assert that the value is not null.

Practice Exercise

Pick any public practice API that returns a list of users. Write one test that extracts the total count of records, a second that collects every email into a List<String> and checks that none are blank, and a third that finds one user by ID with a find expression and asserts on their name. Then refactor the three tests so the response is fetched in a shared setup method. You will quickly see how much cleaner tests become when extraction and assertion are kept separate, and when each test checks one clear idea instead of everything at once.

Ready to master real-world software testing?

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

Explore Course