How to iterate the foreach loop in JMeter

5.7k Views Asked by At

I got the array response from the one API and I passed to the response to the other API. For example: Response from one API is {status:200,data:{name:"Manikyam", selected_products:[1,2,3,4,5]}

I need to iterate the selected_products from the response using foreach or other loop which is suitable of this.

Iterations like:

http://dummy.com/product/1

http://dummy.com/product/2

http://dummy.com/product/3

http://dummy.com/product/4

http://dummy.com/product/5

I tried some ways like using forEach but I haven't any luck. extration from api response

foreach loop

each iteration

1

There are 1 best solutions below

1
On BEST ANSWER

The below answer assumes that your response is a valid JSON looking like:

{
  "status": 200,
  "data": {
    "name": "Manikyam",
    "selected_products": [ 1, 2, 3, 4, 5 ]
  }
}
  1. Add JSON Extractor as a child of the request which returns the above JSON and configure it as follows:

    • Names of created variables: anything meaningful, i.e. product
    • JSON Path Expressions: $.data.selected_products.*
    • Match No: -1
  2. Add ForEach Controller and configure it as follows:

    • Input variable prefix: product
    • Output variable name: product
  3. That's it, if you add a Sampler as a child of the ForEach Controller, the controller will iterate all the variables so you will be able to refer each and every as ${product} where required like http://dummy.com/product/${product}

Demo:

enter image description here