1

I have API call which returns JSON like:

 [
  "Kwara United - Sunshine Stars",
  "Nasarawa United - Rivers United",
  "Plateau United - Enyimba",
  "Abia Warriors - Dakkada",
  "Wikki Tourist - Lobi Stars",
  "Heartland Owerri - MFM FC"
]

from this JSON I create array of opponents:

List values = new ArrayList()

for (int i = 1; i <= 6 ; i++) {
    
    vars.put("opponentA" + i, (vars.get("ID1_" + i)).split(" - ")[0]);
    vars.put("opponentB" + i, (vars.get("ID1_" + i)).split(" - ")[1]);
    values.add((vars.get('opponentA' + i) as String))
    values.add((vars.get('opponentB' + i) as String)) 
    //log.warn(vars.get("opponentA" + i));
    //log.warn(vars.get("opponentB" + i));
} 

vars.put('array', values as String);

Where the variable: array holds all the opponents.

[Kwara United, Sunshine Stars, Nasarawa United, Rivers United, Plateau United, Enyimba, Abia Warriors, Dakkada, Wikki Tourist, Lobi Stars, Heartland Owerri, MFM FC]

How can I tell Loop conroller to loop based on this variable array? enter image description here

And then to pass that dynamic value to JDBC request?

enter image description here

2 Answers 2

2
  1. To get the size of the array use the following __groovy() function:

    ${__groovy(vars.get('array')[1..vars.get('array').length()-2].tokenize('\,').size(),)}
    
  2. To use current value in the loop iterations use the following __groovy() function:

    ${__groovy(vars.get('array')[1..vars.get('array').length()-2].tokenize('\,')[vars.get('__jm__Loop Controller__idx') as int],)}
    

Demo:

enter image description here

More information:

Sign up to request clarification or add additional context in comments.

2 Comments

I confirm its working with Loop and Dammy sampler. How should I pass the value to JDBC into the loop? coz ${__groovy(vars.get('array')[1..vars.get('array').length()-2].tokenize('\,')[vars.get('__jm__Loop Controller__idx') as int],)} is NOT working with it
Your "NOT working" statement doesn't tell anything to me
0

I would use ForEach controller using opponentA variable (uncheck Add "_" before number)

ForEach controller loops through the values of a set of related variables. When you add samplers (or controllers) to a ForEach controller, every sample (or controller) is executed one or more times, where during every loop the variable has a new value. The input should consist of several variables, each extended with an underscore and a number. Each such variable must have a value.

Note: the "_" separator is now optional.

1 Comment

Thanks for the comment, but Is it possible to do with Loop controller?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.