Skip to main content

Iterating over JSON Array Elements using JsonPath in REST Assured

Iterating over JSON Array is same as you iterate using loops on other Array elements. There is nothing special you need to do. Check below example: 

public class ReqRes 
{
	public static String id = "2";
	
	@BeforeClass
	public void setup()
	{
		RestAssured.baseURI = "https://reqres.in/";
		RestAssured.basePath = "api";
	}
	
	
	@Test(enabled=true)
	public void IterateJSONArray()
	{
		Response res = given()
			.queryParam("page", "2")
		.when()
			.get("/users/");
			
		JsonPath js = new JsonPath(res.asString());
		
		int size = js.getInt("data.size()");
		System.out.println("size is: "+size);
		
		
		for(int i = 0; i < size; i++)
		{
			System.out.println(js.getString("data["+i+"].email"));			
		}
	}
}

In above example you can see we have pass the index of array inside getString method using String concatenation and print out the result.

Video Tutorial

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.