So for today I will post the code I am using on Jmeter to get a random index from a list. So, if you have a json with several ids, for example, and you want to choose one of them to use on the next request, you need:
1 – Create a post processor to get the id node from your json response request. As you may know when you use this plugin, jmeter creates automatically the list of variables with the prefix you have chosen and the suffix “__index”. You can see how to use this plugin on my previous post.
2 – Create a Beanshell Sample
3 – Get a random index from this list of ids or you could use this method to get the random id directly. I found easiest get the index r at least it was the first thing which came into my mind.
import java.util.Random; String idSize = vars.get("ids_matchNr"); int max = Integer.parseInt(idSize); int min = 1; int idx = min + (int) (Math.random() * ((max - min) + 1)); String id = vars.get("ids_" + Integer.toString(idx));