

JSON(JavaScript Object Notation)
In RestAssured, JSON (JavaScript Object Notation) is commonly used to send and receive data in API testing. It is lightweight and easy to parse, making it ideal for REST API communication. JSON is the most commonly used data exchange form, independent of any programming language. Data is stored in a structured format in a key-value pair.The keys of JSON will always be a String and the values can be any data type.Key Features of JSON:
Lightweight and easy to parse
Language-independent (works with Java, Python, etc.)
Structured format using key-value pairs
Supports multiple data types (String, Number, Boolean, Array, Object)
Create a User (POST Request)
We will send a JSON payload to create a new user.
API Endpoint
Code Snippet:
public class CreateUserTest {
public static void main(String[] args) {
// JSON Request Body
String requestBody = "{\n" +
" \"name\": \"Alice\",\n" +
" \"job\": \"Software Tester\"\n" +
"}";
// API Call
RestAssured.baseURI="https://reqres.in";
Response response = given().headers("Content-Type" , "application/json")
.body(requestBody)
.when().post("/api/users")
.then()
.statusCode(201) // Validates response code
.extract()
.response();
// Print Response
System.out.println("Response: " + response.getBody().asString())
}
}
Output:
Response: {"name":"Alice","job":"Software Tester","id":"986","createdAt":"2025-03-28T06:29:48.443Z"}
Extracting JSON Response in RestAssured (GET Request)
We can extract values from JSON responses using JsonPath.
What this does:
- Sends a GET request to fetch user details
- Parses the JSON response using JsonPath
- Extracts and prints first_name and email
Code Snippet:
public class GetRequestExample {
public static void main(String[] args) {
RestAssured.baseURI="https://reqres.in";
Response response = given().when().get("/api/users")
.then()
.statusCode(200)
.extract()
.response();
// Convert response to JSON
JsonPath jsonPath = response.jsonPath();
String name = jsonPath.getString("data.first_name");
String email = jsonPath.getString("data.email");
// Print extracted values
System.out.println("Name: " + name);
System.out.println("Email: " + email);
}
}
Output:
Name: [George, Janet, Emma, Eve, Charles, Tracey]
Email: [george.bluth@reqres.in, janet.weaver@reqres.in, emma.wong@reqres.in, eve.holt@reqres.in, charles.morris@reqres.in, tracey.ramos@reqres.in]
Validating JSON Response (Assertions in RestAssured)
We can validate JSON responses using body() and Matchers.
What this does:
- Check if the response has first_name = Janet
- Validates email contains “@reqres.in”
Code Snippet:
public class JsonValidation {
public static void main(String[] args) {
RestAssured.baseURI="https://reqres.in";
given()
.when()
.get("/api/users/2").then()
.statusCode(200) // Validate HTTP response status is 200
.body("data.first_name", equalTo("Janet")) // Validate first name
.body("data.email", containsString("@reqres.in")) // Validate email contains '@reqres.in'
.body("data.last_name", equalTo("Weaver")) // Validate last name
.body("data.id", equalTo(2)) // Validate user ID
.body("data.avatar", containsString("https://reqres.in/img/faces/2-image.jpg")) // Validate avatar URL
.log().all(); // Print full response in the console
}
}
Output:
{
"data": {
"id": 2,
"email": "janet.weaver@reqres.in",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
Conclusion:
JSON is essential in RestAssured for efficient API testing, enabling structured data exchange. We covered sending JSON in POST requests, extracting data from GET responses, and validating responses using assertions. By following this tutorial and gaining hands-on experience, you are equipping yourself with valuable knowledge that can propel your career in Software Testing Cucumber to new heights. With RestAssured, testers can automate API testing, ensuring accuracy, reliability, and high-quality software releases.
Also Read:
Consult Us