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");
Answer from Johan on Stack Overflow
🌐
TOOLSQA
toolsqa.com › rest-assured › read-json-response-body-using-rest-assured
How to Read Json Response Body using Rest Assured?
In this tutorial, we will learn about How to Read JSON Response Body using Rest Assured? and How to Validate Content of a Response Body? Let us continue with the example of Weather web service that we used in the previous tutorials. When we request for the Weather details of a particular city, Server responds by sending the Weather details of the city as the Response Body. Response interface contains two methods to get the Response Body
🌐
Baeldung
baeldung.com › home › http client-side › getting and verifying response data with rest-assured
Getting and Verifying Response Data with REST-assured | Baeldung
October 26, 2025 - Thus, by creating an anonymous inner class extending TypeRef<List>, we explicitly tell REST Assured the exact generic type we want to deserialize the response body into.
🌐
Javadoc.io
javadoc.io › doc › io.rest-assured › rest-assured › 3.0.1 › io › restassured › response › Response.html
Response - rest-assured 3.0.1 javadoc
Bookmarks · Latest version of io.rest-assured:rest-assured · https://javadoc.io/doc/io.rest-assured/rest-assured · Current version 3.0.1 · https://javadoc.io/doc/io.rest-assured/rest-assured/3.0.1 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc...
🌐
Javadoc.io
javadoc.io › doc › com.jayway.restassured › rest-assured › 1.6.1 › com › jayway › restassured › response › ResponseBody.html
ResponseBody - rest-assured 1.6.1 javadoc
Bookmarks · Latest version of com.jayway.restassured:rest-assured · https://javadoc.io/doc/com.jayway.restassured/rest-assured · Current version 1.6.1 · https://javadoc.io/doc/com.jayway.restassured/rest-assured/1.6.1 · package-list path (used for javadoc generation -link option) · ...
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › validate json response with rest assured
Validate JSON Response with REST Assured - Apps Developer Blog
March 23, 2024 - Validates the HTTP response body to check if it contains a specific string value. Validates the JSON content to check if it contains a specific JSON key and value. NOTE: To validate the JSON content, we will use the JsonPath class, which is located in the io.restassured.path.json package.
Find elsewhere
Top answer
1 of 1
8

Just write a simple reusable method to extract values using JSONPath and call the method in your code, here's a sample

Reusable Method :

public static String getJsonPath(Response response, String key) {
    String complete = response.asString();
    JsonPath js = new JsonPath(complete);
    return js.get(key).toString();
}

Test :

public static void main(String[] args) {

    Response res = given().header("X-API-KEY", Config.API_KEY).header("session", this.sessionID)
            .header("username", this.userNameId).queryParam("code", verifiedCode).log().all()
            .get(baseUri + basePath + "/vm1/verifyCode").then().log().all().extract().response();
    String value = getJsonPath(res, "accessToken");
    
    System.out.println(value);
}

Update :

public static String getToken(String key) {
    String res = given().header("X-API-KEY", Config.API_KEY).header("session", this.SessionId)
            .header("username", this.UserName).queryParam("code", verifiedCode).log().all()
            .get(baseUri + basePath + "/vm1 /verifyCode").then().log().all().extract().asString();
    JsonPath js = new JsonPath(res);
    return js.get(key).toString();
}

@Test
public void testAuthValidator() throws InterruptedException, IOException, GeneralSecurityException {
    String sentCode = GmailUtility.getVerificationCode();
    System.out.println(sentCode);
    String Token = getToken("accessToken");
    System.out.println(Token);
    driver = initializeDriver();
    driver.get(env.API_Website);
    AuthApi auth = new AuthApi(driver);
    auth.getAuthorizeButton().click();
    auth.getValueField().sendKeys("Token " + Token);
}

You can get any value using this

🌐
Makeseleniumeasy
makeseleniumeasy.com › 2021 › 02 › 03 › rest-assured-tutorial-67-how-to-assert-full-response-json-body-in-rest-assured
REST Assured Tutorial 67 – How to assert full response JSON body in Rest Assured?
February 3, 2021 - package RestAssuredConcepts; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.hamcrest.Matchers; import io.restassured.RestAssured; public class AssertFullResponseBodyFromExternalFile { public static void main(String[] args) throws IOException { RestAssured // Construct request .given() .log() .all() .baseUri("https://gorest.co.in/public-api/") .basePath("users/{id}") .pathParam("id", "178") // Hit API .when() .get() .then() // Assert response .log() .all() .assertThat() // Pass full expected response body with Hamcrest matchers .body(Matchers.equalTo(new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\src\\test\\resources\\jsonFiles\\UserDetails178.json"))))); } }
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › rest assured get http response body
REST Assured Get HTTP Response Body - Apps Developer Blog
May 15, 2018 - In this short tutorial on REST Assured you will learn how to get the entire HTTP Response Body when testing your RESTful Web Service Endpoint.
🌐
DevQA
devqa.io › parse-json-response-rest-assured
How to Parse JSON Response with REST-assured - DevQA.io
then().contentType(ContentType.JSON).extract().response(); } public static void main(String[] args) { Response response = doGetRequest("https://jsonplaceholder.typicode.com/users"); List<String> jsonResponse = response.jsonPath().getList("$"); System.out.println(jsonResponse.size()); } } The result of the above call would print 10. Note the $ notation which means the root element. In the above example, if we wanted to get the username of all entries, we could use:
🌐
TutorialsPoint
tutorialspoint.com › how-to-verify-a-json-response-body-using-assertions-in-rest-assured
How to verify a JSON response body using Assertions in Rest Assured?
Using Rest Assured, we shall verify the value of the Location in the Response body. ... import org.hamcrest.Matchers; import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import io.restassured.RestAssured; public class NewTest { @Test public void ressponseAssertion() { //base URL RestAssured.baseURI = "https://run.mocky.io"; //GET operation given() .when().get("/v3/6c6ed634-5e78-4b80-94c7-cf17c04c7055").
🌐
James Willett
james-willett.com › rest-assured-extract-json-response
Extracting a JSON Response with REST Assured | James Willett
June 16, 2015 - How to extract a JSON response in REST Assured and then use that response later on in your tests
🌐
Javadoc.io
javadoc.io › doc › io.rest-assured › rest-assured › 3.0.6 › io › restassured › response › ResponseBody.html
ResponseBody - rest-assured 3.0.6 javadoc
Latest version of io.rest-assured:rest-assured · https://javadoc.io/doc/io.rest-assured/rest-assured · Current version 3.0.6 · https://javadoc.io/doc/io.rest-assured/rest-assured/3.0.6 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/io.rest-assured/rest-assured/3.0.6/package-list ·
🌐
Baeldung
baeldung.com › home › web services › asserting rest json responses with rest-assured
Asserting REST JSON Responses With REST-assured | Baeldung
March 11, 2026 - As we saw earlier, REST-assured is an assertion library supporting Hamcrest matchers in its body() method. To use it with the other JSON assertion libraries, we’ve had to extract the response body. Each of the libraries can be used as a Hamcrest matcher.
🌐
TutorialsPoint
tutorialspoint.com › how-to-extract-the-whole-json-response-as-a-string-in-rest-assured
How to extract the whole JSON response as a string in Rest Assured?
import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import io.restassured.RestAssured; public class NewTest { @Test public void getResponseAsString() { //base URL RestAssured.baseURI = "https://run.mocky.io/v3"; String r = RestAssured.given().when() //get request .get("/cd3a7e12-9057-4b51-bf2d-a2d4e2ddad8d") //get response as string .then().extract().response().asString(); System.out.println(r); } }
🌐
GitHub
github.com › rest-assured › rest-assured › wiki › usage
Usage · rest-assured/rest-assured Wiki · GitHub
REST Assured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests and can be used to validate and verify the response of these requests.
Author   rest-assured
🌐
Medium
medium.com › @m4manishd › mastering-response-handling-with-rest-assured-in-automation-testing-72872696da96
Mastering Response Handling with REST Assured in Automation Testing | by Manish Dhakeria | Medium
November 23, 2024 - Body: The actual data returned (e.g., JSON, XML). REST Assured’s Response interface provides methods to access and validate these components seamlessly. The Response object contains methods to retrieve response details. import io.restassu...
🌐
Medium
medium.com › @kaweesha › api-automation-with-rest-assured-bd1e250a098c
API automation with REST Assured. REST Assured is a powerful Java API… | by kaweesha wijayawickrama | Medium
May 12, 2020 - For a POST (and PUT) call we have to pass a request body. In REST Assured we can pass a String value to the request body. Alternatively, we can pass a Java object as the request body.