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" }
}
| Path | Returns |
|---|---|
page | 1 |
data[0].email | The first user's email |
data.first_name | A list of all first names |
data.size() | Number of users in the array |
data.find { it.id == 8 }.email | Email of the user whose id is 8 |
data.findAll { it.id > 7 }.first_name | Names of users with id above 7 |
support.url | A 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 asgetIntandgetList.- 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
getIntorgetLong. - 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.