REST Assured Iterating Over JSON Array: how to select randomly an email from the email list

Query:  Hi! Thanks for your tutorial.
Based on this example, I have a question -> After getting all emails, how to select randomly an email from the email list? Hope to receive your reply. Thank you!

User: caocao cup

Source: Youtube ProgramsBuzz

Video Title: Iterating over JSON Array Elements using JsonPath in REST Assured

Video URL: https://www.youtube.com/watch?v=M_6COwlmODU

Hi there,

You can get a random mail out the list using :

  • ArrayList and Random from javautils
  • Store the loop iteration of the email list into the array list we declared outside.
  • With the use of Random util we can get the random mail out of the list.
@Test(enabled=true)
	public void ArrayIterate()
	{
		Random r = new Random();
		ArrayList<String> ar = new ArrayList<String>();
		Response res = 
		given()
//			.queryParam("page", "2")
	    .spec(reqSpec)

		.when()
			.get("/users/");
			
		JsonPath js = new JsonPath(res.asString());
		
		int size = js.getInt("data.size()");
		for(int i = 0; i < size; i++)
		{
			
			String emailList = js.getString("data["+i+"].email");
			
		ar.add(emailList);

			
		}	
		System.out.println(ar.get(r.nextInt(ar.size())));
		
	}
	

}