

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get a value from a nested list in Rest Assured?
We can get a value from a nested list in Rest Assured. This is done with the help of the extract method. To grab the item, we have to use the path method(after the extract method) and pass the item in the response we want to obtain.
We shall first send a GET request via Postman on a mock API URL and go through its Response having a nested list.
Example
Code Implementation
import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import java.util.ArrayList; import io.restassured.RestAssured; import io.restassured.http.ContentType; public class NewTest { @Test public void getRequest() { //base URL RestAssured.baseURI = "https://run.mocky.io/v3"; RestAssured.basePath = "/23ab8486-7d8d-41e4-be27-d603c767d745"; //response from GET request given() .when().get().prettyPrint(); //extract values from Response String name = given().contentType(ContentType.JSON).get() .then().extract().path("name"); System.out.println(name); String age = given().contentType(ContentType.JSON).get() .then().extract().path("age"); System.out.println(age); //extract values from a nested list in Response ArrayList<String> s = given().contentType(ContentType.JSON).get() .then().extract().path("subjects"); for(String subject: s) { System.out.println(subject); } } }
Output
- Related Questions & Answers
- How to get a JSON field in a nested JSON using Rest Assured?
- How to get a JSON array field in a nested JSON using Rest Assured?
- How to get the size of an array within a nested JSON in Rest Assured?
- How to get the response time of a request in Rest Assured?
- How to parse a JSON response and get a particular field from the response in Rest Assured?
- How to update the value of a field in a request using Rest Assured?
- Explain how to get the size of a JSON array response in Rest Assured.
- How to transform the response to the Java list in Rest Assured?
- What is Rest Assured?
- How to handle static JSON in Rest Assured?
- How to validate XML response in Rest Assured?
- How to create a project with Cucumber and Rest Assured dependencies?
- How to get JSON fields(nodes) based on conditions using Rest Assured?
- How to verify a JSON response body using Assertions in Rest Assured?
- How to validate the response time of a request in Rest Assured?
Advertisements