Search code examples
groovyjmeter

How to add JSON extractor value from HTTP request to array on each request itteration


So i have a bit tricky ( for me at least ) situation. I have list of 100 users and need to get some address information from each of 100 users. Unfortunately our REST API is not allowing to obtain this information via one GET request, rather i need to call 100 GET requests ( one GET request in a loop ) and to pass the ids of 100 users in order to get information about address.

So the question is: How would i use JSON Extractor value ( to get address field value from response ) and put it in array and to repeat this process for all 100 users. And to later use that array to obtain information about size of array and to check if certain element of that array contains certain data?


Solution

  • Given you have groovy tag I'll provide solution with Groovy.

    1. def addresses = vars.getObject('addresses') ?: [] - get array of addresses from JMeter Variables or return an empty array if the variable is null (first execution)
    2. def address = new com.jayway.jsonpath.JsonPath().read(prev.getResponseDataAsString(), '$.your-json-path-expression-here') - extract address from the previous sampler response using JsonPath
    3. addresses.add(address) - add the address to addresses array
    4. vars.putObject('addresses', addresses) - store the array into JMeter Variables
    5. def size = addresses.size() - get the array size
    6. def containsData = addresses[5].contains('the data you are looking for') - checks if 6th address contains some data

    More information on these vars, prev and other shorthands: Top 8 JMeter Java Classes You Should Be Using with Groovy