By Shivam Singh, founder of TesterRank · Updated September 2026
Sometimes you do not just want to validate — you want to extract data and use it. Get the user ID from a create response and use it in the next request. Get a token from login and pass it to subsequent calls. REST Assured gives you jsonPath() for this.
JsonPath reads values out of a JSON response with short paths like title or address.city. Here is the basic way to use it.
import io.restassured.response.Response;
import io.restassured.path.json.JsonPath;
@Test
public void testJsonPathExtraction() {
Response response =
given()
.when()
.get("/posts/1")
.then()
.statusCode(200)
.extract().response();
// Extract single values
int id = response.jsonPath().getInt("id");
String title = response.jsonPath().getString("title");
int userId = response.jsonPath().getInt("userId");
System.out.println("ID: " + id); // 1
System.out.println("Title: " + title); // sunt aut facere ...
System.out.println("User: " + userId); // 1
}When the response is a list, you can pick one item by position, or collect one field from every item into a Java list.
@Test
public void testExtractFromArray() {
Response response =
given()
.when()
.get("/posts")
.then()
.statusCode(200)
.extract().response();
// First element
String firstTitle = response.jsonPath().getString("[0].title");
// Last element
String lastTitle = response.jsonPath().getString("[-1].title");
// All titles as a List
List<String> allTitles = response.jsonPath().getList("title");
System.out.println("Total titles: " + allTitles.size()); // 100
// All IDs as a List of Integers
List<Integer> allIds = response.jsonPath().getList("id", Integer.class);
// Filtered — all posts by userId 1
List<Integer> user1Posts = response.jsonPath().getList("findAll { it.userId == 1 }.id");
System.out.println("User 1 posts: " + user1Posts.size()); // 10
}| JsonPath Expression | Returns | Usage |
|---|---|---|
| "id" | Single value | getInt("id") → 1 |
| "[0].title" | First element field | getString("[0].title") → first title |
| "title" | All titles from array | getList("title") → ["title1", ...] |
| "size()" | Array length | getInt("size()") → 100 |
| "findAll { it.userId == 1 }" | Filtered array | getList(...) → matching items |
| "find { it.id == 5 }" | First matching item | getMap(...) → single item |
| "max { it.id }" | Max value item | getMap(...) → item with highest id |
| "collect { it.title }" | Transform to list | getList(...) → titles only |
If you only need one value, extract it straight from the request chain without saving the whole response first.
@Test
public void testInlineExtraction() {
// Extract a single value directly — no Response variable needed
int postId =
given()
.when()
.get("/posts/1")
.then()
.statusCode(200)
.extract()
.jsonPath().getInt("id"); // Returns just the int
System.out.println("Post ID: " + postId); // 1
// Extract as String
String title =
given()
.when()
.get("/posts/1")
.then()
.extract()
.jsonPath().getString("title");
}This is the power move. Instead of extracting individual fields, deserialize the entire response into a Java object. Jackson does the heavy lifting. Your Post class from earlier? REST Assured fills it with data from the response.
@Test
public void testDeserializeToPojo() {
// Single object
Post post =
given()
.when()
.get("/posts/1")
.then()
.statusCode(200)
.extract()
.as(Post.class); // Jackson deserializes JSON to Post
System.out.println("Title: " + post.getTitle());
System.out.println("User ID: " + post.getUserId());
Assert.assertEquals(post.getId(), 1);
Assert.assertNotNull(post.getTitle());
}
@Test
public void testDeserializeArray() {
// Array of objects
Post[] posts =
given()
.when()
.get("/posts")
.then()
.statusCode(200)
.extract()
.as(Post[].class); // Array of POJOs
System.out.println("Total posts: " + posts.length); // 100
Assert.assertEquals(posts[0].getUserId(), 1);
}Use .extract().as(Post.class) when you need the entire object. Use .extract().jsonPath().getString("title") when you need just one field. Do not deserialize the whole response if you only need the ID.
For deserialization to work, your POJO must have: 1) a no-arg default constructor, 2) matching field names (or @JsonProperty annotations), and 3) Jackson on the classpath. If you get SerializationException, check these three things first.
Q: How do you extract response data in REST Assured?
A: Three approaches: 1) jsonPath() — extract specific fields using path expressions. Use getInt("id"), getString("name"), getList("items") on the response object. 2) Inline extraction — chain .extract().jsonPath().getInt("id") directly after .then() without storing the Response. 3) POJO deserialization — use .extract().as(MyClass.class) to convert the entire response to a Java object using Jackson. JsonPath also supports Groovy expressions for filtering: findAll { it.price > 100 }. Use jsonPath for single values, POJO deserialization for complex responses.
Key Point: Use jsonPath().getInt(), getString(), getList() for specific values. Use .extract().as(Pojo.class) for full deserialization. JsonPath supports Groovy filtering with findAll/find.