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
- Send a GET request to
/users/1and store theResponse. - Call
jsonPath()on it to get a queryable view of the body. - Read the
namefield and then theemailfield withgetString(). - Print them on two lines, labelled
Name:andEmail:, in that order.
Common mistakes
A misspelt path returns null
Asking for
nmaeinstead ofnameraises no error withgetString(). You simply getnull, 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
/userslist,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 aFloat, which is not equal to thedoubleliteral you typed. UsegetDouble()when the comparison needs adouble.
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.
Related lessons
- Your First REST Assured TestThe first GET request and how
baseURIdecides where it goes. - Extract Single ValueRead fields straight out of a validated chain with
extract(). - Deserialize Response to POJOMap the same user JSON onto a Java class instead of reading fields one by one.
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.
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
}
}