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

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
vlatko606
  • 909
  • 1
  • 13
  • 21

2 Answers2

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:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • 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 – vlatko606 Jun 28 '21 at 07:01
  • Your "NOT working" statement doesn't tell anything to me – Dmitri T Jun 28 '21 at 12:38
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.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233