Reputation: 2074
This is response output from a request in jmeter,
{
"item": [
{
"id": 4,
"name": "American Samoa",
"capital": "Pago Pago",
"region": "Oceania",
"subregion": "Polynesia",
"population": 57100,
"area": 199,
"numericCode": "016"
},
{
"id": 249,
"name": "Zimbabwe",
"capital": "Harare",
"region": "Africa",
"subregion": "Eastern Africa",
"population": 14240168,
"area": 390757,
"numericCode": "716"
},
{
"id": 250,
"name": "Aland Islands",
"capital": "Mariehamn",
"region": "Europe",
"subregion": "Northern Europe",
"population": 28875,
"area": 1580,
"numericCode": "248"
},
{
"id": 256,
"name": "Test Name",
"capital": "Capital",
"region": "NF",
"subregion": "LoadTesting",
"population": 10,
"area": 10,
"numericCode": "9"
},
{
"id": 257,
"name": "Test Name",
"capital": "Capital",
"region": "NF",
"subregion": "LoadTesting",
"population": 10,
"area": 10,
"numericCode": "9"
},
{
"id": 258,
"name": "Test Name",
"capital": "Capital",
"region": "NF",
"subregion": "LoadTesting",
"population": 10,
"area": 10,
"numericCode": "9"
},
{
"id": 259,
"name": "Test Name",
"capital": "Capital",
"region": "NF",
"subregion": "LoadTesting",
"population": 10,
"area": 10,
"numericCode": "9"
},
{
"id": 260,
"name": "Test Name",
"capital": "Capital",
"region": "NF",
"subregion": "LoadTesting",
"population": 10,
"area": 10,
"numericCode": "9"
}
],
"statusCode": 200
}
The output is in the JSON format, and I want to retrieve all the id which have "name": "Test Name","capital": "Capital","region": "NF","subregion": "LoadTesting" and store them in an array.
I am using jmeter 5.2, with JSR223 post-processor with javascript. I am getting error with the following script.
var responseData = prev.getResponseDataAsString();
var countryDetails = responseData.Items;
log.info(prev.getResponseDataAsString());
log.info(countryDetails);
Upvotes: 0
Views: 2151
Reputation: 2978
You can use JSON Extractor also, use the below as JSON Path expressions and Match No to -1
:
.item[?(@.name=='Test Name' && @.capital=='Capital' && @.region=='NF' && @.subregion=='LoadTesting')].id
You will get below results:
Upvotes: 0
Reputation: 168072
Don't use JavaScript in the JSR223 Test Elements and don't use anything but Groovy there. Be aware that in JMeter's JSR223 Test Elements you don't have fancy JSON object as it's not a part of Nashorn engine so your approach will not work in any case.
The relevant Groovy code to print items
attribute value to the jmeter.log file would be something like:
def items = new groovy.json.JsonSlurper().parse(prev.getResponseData()).item
def countryDetails = new groovy.json.JsonBuilder(items).toPrettyString()
log.info(countryDetails)
Demo:
More information:
Upvotes: 2