Skip to content
CodeKruPro

Reading the Response Body

2/62

Reading the Response Body

REST APIs return data in JSON format. REST Assured provides jsonPath() to easily extract values from JSON responses.

The API at /users/1 returns a single user object:

{"id": 1, "name": "John Doe", "email": "john@example.com", "age": 30, "city": "New York"}

Task:

  1. Make a GET request to /users/1
  2. Extract and print the name and email fields using jsonPath().getString()

Expected output:

Name: John Doe
Email: john@example.com

About this lesson

REST Assured jsonPath getString: reading JSON fields from a response body

Learn REST AssuredBeginnerGetting Started

How it works

A JSON API answers with text, and a test needs Java values. response.jsonPath() parses that text once and returns an io.restassured.path.json.JsonPath, an object you can query by path. A path is a field name for a top-level key, dot-separated names for nested objects, and an index in square brackets for array elements.

JsonPath offers typed getters. getString() converts whatever it finds into a String, getInt() returns a primitive int, and the generic get() returns whatever type the parser chose: for user 1, get("age") comes back as an Integer. response.path() is a shortcut that reads one value without naming jsonPath() at all.

Missing data behaves differently per getter. A path that matches nothing makes getString() return null, but getInt() on the same path throws a NullPointerException, because it cannot unbox a null into an int.

About the assignment

The single-user endpoint answers with one JSON object holding an id, a name, an email, an age and a city. You request user 1 and read two of those string fields out of the body, the basic move behind every data check.

What you will do

  1. Send a GET request to /users/1 and store the Response.
  2. Call jsonPath() on it to get a queryable view of the body.
  3. Read the name field and then the email field with getString().
  4. Print them on two lines, labelled Name: and Email:, in that order.

Common mistakes

  • A misspelt path returns null

    Asking for nmae instead of name raises no error with getString(). You simply get null, and a print shows the word null after your label, so check spelling against the real body.

  • Reading a whole array as one value

    On the /users list, getString("name") does not pick one user. It joins every name into a single bracketed string, which looks plausible but is not a field value. Index into the array first.

  • Comparing decimals with the wrong type

    A decimal such as a product price comes back from get() as a Float, which is not equal to the double literal you typed. Use getDouble() when the comparison needs a double.

In a real test suite

Real suites rarely print values. They feed what jsonPath() returns into assertions, or they skip the manual read and use body() matchers in a then() block. When a test needs many fields at once, mapping the JSON to a Java class with as() keeps the code shorter than a dozen separate getString() calls.

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;

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

        // TODO: GET /users/1
        // TODO: Extract "name" using response.jsonPath().getString("name")
        // TODO: Extract "email" similarly
        // TODO: Print both in the format shown
    }
}