简体   繁体   中英

Jmeter - How to read multiple values returned by GET request in the form of array in Json format and write it to a CSV file

Jmeter - I have multiple values ( 250 values approx as a minimum ) returned from a Get Request in the form of array in json format , i need pass each of the value in the array as a parameter in next GET request

Request 1

> Get http://xxx/store1?

Response in json format looks like this

{"store1":"peirre","inventorylist":["item1","item2","item3"..........."item250"]}

I will need to use each one of item in next GET request like this to get is feautures like price , available quantity,production site etc.,

Request 2

Get https://xxx/store1/item1?
Get https://xxx/store1/item2?
Get https://xxx/store1/item3?

It would be easy when i can read the response and write each value in the array to CSV file so that my next get request would simply read the CSV file and fire all the requests

Is there a way to achieve this ???

Thank you in advance

  1. Add JSON Extractor as a child of the request which returns the above JSON
  2. Configure it as follows:

    • Names of created variables: anything meaningful, ie item
    • JSON Path Expressions: $.inventorylist.*
    • Match No.: -1
  3. Add ForEach Controller after the first HTTP Request sampler and configure it as follows:

    • Input variable prefix: whatever you used as "Names of created variables" in the JSON Extractor, ie item
    • Output variable name: anything meaningful, ie current_item
  4. Add HTTP Request sampler as a child of the ForEach Controller and use https://xxx/store1/${current_item} in "Path" field - it will iterate through all the "item"s

    JMeter JSON提取器和ForEach控制器

  1. Add a Json Extarctor to the first Get Request with the following properties 在此处输入图片说明

2.Add a JSR223 post processor and initialize a counter as shown below 在此处输入图片说明

  1. Add a while controller and place your get request in while controller.

add the following condition

${__javaScript(parseInt(vars.get("counter"))<=parseInt(vars.get("List_matchNr")))}

as shown below

在此处输入图片说明

  1. To your second get request add a JSR223 post processor and increment the counter as shown below

    int counter = Integer.parseInt(vars.get("counter")) +1;

    vars.put("counter",Integer.toString(counter));

在此处输入图片说明

  1. Use ${__V(List_${counter})} to replace hardcoded value

This loop will run through the match number and sends a request with each item to server

在此处输入图片说明

For more information on while loop please follow the link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM