How to get a JSON field in a nested JSON using Rest Assured?

Rest AssuredDynamic ProgrammingProgramming

We can get a JSON field in a complex nested JSON using Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string.

We shall send a GET request via Postman on a mock API URL and observe its Response.

Using Rest Assured, let us get the value of the Price field having the value as $150. It is part of the Items. We shall get the value of the Item Count field by traversing the path - Items.Price.


Code Implementation

import static io.restassured.RestAssured.given;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
public class NewTest {
   public void jsonValue() {

      //base URI with Rest Assured class
      RestAssured.baseURI = "";

      //obtain Response from GET request
      Response res = given()

      //convert JSON to string
      JsonPath j = new JsonPath(res.asString());

      //get a field value from nested JSON
      String p = j.getString("Items.Price");
      System.out.println("Price is: " + p);


Updated on 17-Nov-2021 12:41:22