How to extract Response in Rest Assured by JSONPath

  

What is JsonPath?

JsonPath is similar to XPath for XML, but is used to extract data from JSON. Rest Assured provides built-in support for it.















Steps to Extract Response Using JsonPath in Rest Assured:

  • Send GET Request – Use given().get(...).
  • Extract Response – Use .extract().response().
  • Use JsonPath – Use response.jsonPath() to extract values.

URL:

https://reqres.in/api/users?page=2





Sample Java Code using Rest Assured:


import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.path.json.JsonPath;

import java.util.List;

public class JsonPathExample {

    public static void main(String[] args) {

        // Step 1: Set Base URI
        RestAssured.baseURI = "https://reqres.in";

        // Step 2: Send GET request and get response
        Response response = RestAssured
                .given()
                .when()
                .get("/api/users?page=2")
                .then()
                .statusCode(200)
                .extract()
                .response();

        // Step 3: Parse response with JsonPath
        JsonPath jsonPath = response.jsonPath();

        // Step 4: Extract specific values
        int total = jsonPath.getInt("total");
        int perPage = jsonPath.getInt("per_page");
        String firstEmail = jsonPath.getString("data[0].email");
        List<String> allEmails = jsonPath.getList("data.email");

        // Step 5: Print the extracted values
        System.out.println("Total users: " + total);
        System.out.println("Users per page: " + perPage);
        System.out.println("First user's email: " + firstEmail);
        System.out.println("All emails:");
        for (String email : allEmails) {
            System.out.println(email);
        }
    }
}




Common JsonPath Queries:


TaskJsonPath Expression
Total number of usersjsonPath.getInt("total")
Email of first userjsonPath.getString("data[0].email")
List of all emailsjsonPath.getList("data.email")
ID of last userjsonPath.getInt("data[-1].id")





Maven Dependencies:


<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

 

To test a single response header in a GET API using Rest Assured in Java, you can follow these steps:














We want to:

  • Send a GET request to https://reqres.in/api/users?page=2

  • Extract and validate a specific response header (example: Content-Type)


What is a Response Header?

response header provides metadata about the server response. Common headers include:

  • Content-Type

  • Content-Encoding

  • Date

  • Server

  • Connection



Steps in Rest Assured

  • Set the base URI
  • Send a GET request
  • Extract the specific header
  • Validate it using assertion



Sample Java Code Using Rest Assured


import io.restassured.RestAssured;
import io.restassured.response.Response;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class GetResponseHeaderTest {

    public static void main(String[] args) {
        
        // Step 1: Set base URI
        RestAssured.baseURI = "https://reqres.in";

        // Step 2: Send GET request and get response
        Response response = RestAssured
                .given()
                .when()
                .get("/api/users?page=2")
                .then()
                .extract()
                .response();

        // Step 3: Print and verify a single header, e.g. Content-Type
        String contentType = response.getHeader("Content-Type");
        System.out.println("Content-Type header is: " + contentType);

        // Step 4: Assert the header
        assertThat("Header validation failed", contentType, equalTo("application/json; charset=utf-8"));
    }
}





Explanation:

  • getHeader("Content-Type") fetches the value of the specified header.
  • assertThat(..., equalTo(...)) checks if the header value is as expected.




Maven Dependency:


<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.4.0</version>
    <scope>test</scope>
</dependency>