I'm having a JSON data, from which I'm able to fetch some data from JSON Extractor post-processor. And the data looks something like this,
group_1 = ["Scooter"]
group_2 = ["Bus","Jeep","Car"]
group_ALL = ["Scooter"],["Bus","Jeep","Car"]
group_matchNr = 2
So I want to fetch all this data (or accumulate) in only one array variable,
Example : groups=["Scooter","Bus","Jeep","Car"]
so that I loop it through all the individual data in the api calls in a foreach loop.
example api call in foreach loop: https://vehicles.com/${groups}
My question is, I'm trying to write the groovy script to get all the data in groups variable. But unable to. I need your help in this and to configure the foreach controller also.
Groovy Script I tried in JSR223 Postprocessor which did not work:
import groovy.json.JsonSlurper;
groups= vars.get("group_ALL");
def jsonSlurper = new JsonSlurper();
jsonData = JSON.stringify(vars.get("groups"))
String tempRemoveBr = jsonData.replaceAll("[","");
String tempRemoveBr1 = tempRemoveBr.replaceAll("]","");
String[] arrVehicles = tempRemoveBr1.split(",")
for (int i=0;i<arrVehicles.size();i++)
{
log.info("arrVehicles[i]")
}
vars.put("arrVehicles",arrVehicles)
foreach controller configured as :
Input variable : arrVehicles
start index : 0
output variable name : vehicleN
Most probably you could achieve it using JSON JMESPath Extractor, however I cannot come up with a proper configuration without seeing your full response.
If you want to process existing variables here is a Groovy script you could use:
def group1 = new groovy.json.JsonSlurper().parseText(vars.get('group_1'))
def group2 = new groovy.json.JsonSlurper().parseText(vars.get('group_2'))
def arrVehicles = group1 + group2
vars.put('arrVehicles', new groovy.json.JsonBuilder(arrVehicles).toString())
More information: