Skip to content
CodeKruPro

Extract Single Value

12/62

Extract Single Value

The .extract() method lets you pull data out of a validated response:

String name = given().get("/users/1")
    .then().statusCode(200)
    .extract().path("name");

This is different from just jsonPath().getString() because it chains with validation.

Task:

  1. GET /users/2, validate status 200
  2. Extract and print the name, email, and city

Expected output:

Name: Jane Smith
Email: jane@example.com
City: London

About this lesson

REST Assured extract(): getting a path value out of a validated response

Learn REST AssuredIntermediateJSON Path Extraction

How it works

A chain that ends in then() checks a response, but a test often needs data from it as well, for example an id for the next request. extract() switches the chain from checking to reading and returns an ExtractableResponse. From there path() reads one value, jsonPath() gives a full parser, statusCode() returns the code, and response() hands back the plain Response object.

Order matters. Every check before extract() runs first, and if one fails it throws an AssertionError, so the extraction never happens. That gives a useful guarantee: whatever you extracted came from a response that already passed its checks.

path() is generic, so its result takes the type of the variable it is assigned to. If the JSON value has another type, Java reports a ClassCastException when the value is assigned, not at compile time.

About the assignment

User 2 is the record this time. You request it, confirm the status is 200, and pull three string fields out of that validated response, printing each on its own labelled line.

What you will do

  1. Request /users/2 with a given() chain and assert 200 inside then().
  2. Call extract() and keep either the whole response via response() or a parser via jsonPath().
  3. Read name, email and city from what you kept.
  4. Print them labelled Name:, Email: and City:, in that order.

Common mistakes

  • Repeating the request for each field

    Calling the full chain three times with three different path() arguments sends three requests. Extract once, keep the result, and read every field from that.

  • Assigning a number to a String

    Extracting age with path() into a String variable compiles but throws ClassCastException, because the value is an Integer. Use the right variable type or getString().

  • Expecting extract() to skip the checks

    If the status assertion fails, nothing is extracted. The error comes from the check, so read that message before looking at your extraction code.

In a real test suite

Extraction is how API tests chain steps together: create a record, extract its id, then use it in a GET, PUT or DELETE. In JUnit 5 a helper method often wraps the request, checks and extraction, returning only the value the next step needs.

To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /users/2, validate status 200, extract name, email, city
        // TIP: You can extract the whole response first, then use jsonPath()
        //      OR chain .extract().path("fieldName")
    }
}