How to Parse JSON Response with REST-assured

In this API Testing tutorial, we take a look at how to parse JSON response and extract information using the REST-assured library.

When testing an API, you typically make a request to a resource, (e.g. via a GET or POST request). The server comes back with a response and then you do some assertions on the response.

How to Parse JSON Response

For this tutorial, I will be using JSONPlaceholder which is a fake online REST API for Testing and Prototyping. JSONPlaceholder is a free online REST service that you can use whenever you need some fake data.

More specifically, I will be using the users endpoint jsonplaceholder.

Request and Response

When we make a GET request to the above resource, we get a JSON response which contains a list of users. This list is represented as a JSON Array. Each array has a structure like this:

    id: 1,
    name: "Leanne Graham",
    username: "Bret",
    email: "",
    address: {
        street: "Kulas Light",
        suite: "Apt. 556",
        city: "Gwenborough",
        zipcode: "92998-3874",
        geo: {
            lat: "-37.3159",
            lng: "81.1496"
    phone: "1-770-736-8031 x56442",
    website: "",
    company: {
        name: "Romaguera-Crona",
        catchPhrase: "Multi-layered client-server neural-net",
        bs: "harness real-time e-markets"

Therefore, in the full response, there will be ten records in the array, each having the same JSON structure, but with different values.

Now, let’s start by parsing and extracting some values from the JSON.

The first test would typically be to count the number of records in the array, so let’s start with that.

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.parsing.Parser;
import io.restassured.response.Response;

import java.util.List;

import static io.restassured.RestAssured.given;

public class RestTest {

    public static Response doGetRequest(String endpoint) {
        RestAssured.defaultParser = Parser.JSON;

            given().headers("Content-Type", ContentType.JSON, "Accept", ContentType.JSON).

    public static void main(String[] args) {
        Response response = doGetRequest("");

        List<String> jsonResponse = response.jsonPath().getList("$");


The result of the above call would print 10. Note the $ notation which means the root element.

Parsing JSON Arrays and Lists

In the above example, if we wanted to get the username of all entries, we could use:

String usernames = response.jsonPath().getString("username");

This would print the array like:

[Bret, Antonette, Samantha, Karianne, Kamren, Leopoldo_Corkery, Elwyn.Skiles, Maxime_Nienow, Delphine, Moriah.Stanton]

If we then want to get the username of the first entry we could use:

String usernames = response.jsonPath().getString("username[0]");

This would print the first username:


Using a List we can use:

List<String> jsonResponse = response.jsonPath().getList("username");

This would print the first username:


Parsing JSON ArrayList and HashMap

Looking at the above JSON structure, the company is actually a map. If we only had one record, we could use:

Response response = doGetRequest("");

Map<String, String> company = response.jsonPath().getMap("company");

which would print:


But if the response returns an array and we want to extract the first company name, we could use:

Response response = doGetRequest("");

Map<String, String> company = response.jsonPath().getMap("company[0]");

Alternatively, we could use:

Response response = doGetRequest("");

List<Map<String, String>> companies = response.jsonPath().getList("company");

both of which will print: