How to get count of elements Json Response using JMeter - jmeter

How to get count of Json elements from Json Response using JMeter
For example: from below response the count is 3. How to retrieve this value ?
[{
"key": "1",
"description": "Goods"
}, {
"key": "2",
"description": "services"
}, {
"key": "3",
"description": "tax"
}
]

You can use JSON JMesPath Extractor and length() function, configuration would look like:
and refer extracted array length as ${foo} JMeter Variable where required.
Alternatively go for JSR223 PostProcessor and the following Groovy code
vars.put('foo', new groovy.json.JsonSlurper().parse(prev.getResponseData()).size() as String)

Related

JMeter Script to extract an id from Json response

I need to extract id which as Status 1. How can I achieve it.
Example: id (abc) in below example as its status is 1
''''
{
"Documents": [
{
"id": "abc",
"Status": 1,
},
{
"id": "pgr",
"Status": 2,
},
{
"id": "abc",
"Status": 2,
}
]
}
''''
Thanks
You can do this using JSON Extractor, the relevant JSONPath query would be something like:
$..[?(#.Status == 1)].id
Demo:
More information:
JSONPath Filter Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Parallel execution of Samplers in JMeter with different URL parameters

There is "ForEach" controller in JMeter which takes an array of items and executes a sampler with each item. This is very useful when you need to execute a sampler with different parameters based on data received in previous request. However, "ForEach" controller runs samplers one after the other.
What I am looking for is, execute the samples in parallel. There is a plug in available in JMeter called, "bzm - Parallel Controller". However, this doesn't accept any input variable like "ForEach" controller does.
For example, I have following data in database. Author along with their books.
[
{
"firstName": "William",
"lastName":"Shakespeare",
"Title": "Mr",
"id": "1",
"books": [
{
"id": "WS1",
"title": "King John",
"year":"1596"
},
{
"id": "WS2",
"title": "Julius Caesar",
"year": "1599"
},
{
"id": "WS3",
"title": "Romeo and Juliet",
"year": "1595"
}
],
"Nationality": "English"
},
{
"firstName": "Sidney",
"lastName":"Sheldon",
"Title": "Mr",
"id": "2",
"books": [
{
"id": "SS1",
"title": "The Naked Face",
"year":"1969"
},
{
"id": "SS2",
"title": "A Stranger in the Mirror",
"year": "1976"
},
{
"id": "SS3",
"title": "Bloodline",
"year": "1977"
}
],
"Nationality": "American"
},
{
"firstName": "Eiichiro",
"lastName":"Oda",
"Title": "Mr",
"id": "3",
"books": [
{
"id": "EO1",
"title": "Wanted",
"year":"1992"
},
{
"id": "EO2",
"title": "Ikki Yako",
"year": "1993"
},
{
"id": "EO3",
"title": "Monsters",
"year": "1994"
}
],
"Nationality": "Japanese"
}
]
In my JMeter Test plan, I have defined a CSV Data Set Config file to store the ids of all authors in my system.
And then, there is a Thread Group. Inside thread group, I have a HTTP Sampler, GET /authors/{id}/books. for Example GET /authors/1/books. This will get all the books written by author "William Shakespeare".
Using JSON Extractor, I can capture the array of book ids returned by GET /authors/{id}/books.
There are 3 books with ids WS1, WS2 and WS3. Now for each of the books, I need to run another HTTP Sampler
PUT /books/WS1
PUT /books/WS2
PUT /books/WS3.
When I use ForEach controller, I can specify the input variable which was captured from previous JSON extractor. And it loops through each PUT request, for each book id. I want to do the same, but in PARALLEL, not sequential.
Does anyone know how to achieve this? or we have to write custom groovy/BeanShell script for this? If custom Groovy/BeanShell script is the only way, can you please tell me how to write this
Many thanks
You can do that by placing each test in a separate threadGroup.
You have a flag in the main test plan that controls if threads are executed in parallel or consecutively.
You're using not very correct test element, consider switching to Parallel Sampler instead.
Given you have the following JMeter Variables:
book_1=WS1
book_2=WS2
book_3=WS3
book_matchNr=3
You can add a Parallel Sampler and configure it to hit URLs containing ${book_1}, ${book_2} and ${book_3}
All the URLs to Retrieve will be executed in parallel:
More information: How to Use the Parallel Controller in JMeter
You could dynamically add URLs to Parallel HTTP Requests sampler.
Add JSR223 PreProcessor like a child to Parallel HTTP Requests sampler.
To script area add something like that:
String url = "https://examle.url.com/?book=";
1.upto(vars.get('book_matchNr') as int, {
index -> {
sampler.addURL(url + vars.get('book_' + index))
}
});

Jmeter-Regular expression extraction-how to get the value of attribute name

I have the below response from Rest service. I need do capture the value of dSecurityGroup which is "TEST" and pass it to next request. Can yu help on this
"GenericResponse": {
"Service": {
"IdcService": "CHECKIN_UNIVERSAL",
"Document": {
"Field": [
{
"name": "xIPM_APP_1_9:isSetDefault",
"value": "1"
},
{
"name": "IdcService",
"value": "CHECKIN_UNIVERSAL"
},
{
"name": "dSecurityGroup",
"value": "TEST"
},
{
"name": "xIPM_APP_1_6:rule",
"value": "IpmApp_1_Fields_Hide"
},
{
"name": "dpTriggerField",
"value": "xIdcProfile"
},
]
}
}
}
Your JSON response is invalid. check with https://jsonformatter.curiousconcept.com/ and update the correct JSON
Your response is JSON therefore it doesn't make sense to use Regular Expression Extractor.
Consider using JSON Extractor instead, it allows using JsonPath queries which provide handy way to extract the "interesting" values from JSON responses.
In your case the relevant query would be something like:
$..[?(#.name == 'dSecurityGroup')].value
Demo:
More information: API Testing With JMeter and the JSON Extractor

JMeter Regular Expression to retrieve multi-line records

{
"GROUP1": {
"USER1": {
"name": {
"title": "Mr",
"surname": "David",
"firstname": "Morgan",
"othernames": "Joseph"
},
"dateofbirth": {
"year": "1934",
"month": "12",
"day": "28"
}
}
}
}
Can anyone share what the JMeter Regular Expression is to retrieve everthing between {} after "name": please? Thanks
Use instead JSON path extractor or JSR223 PostProcessor (or more) see example
Parsing JSON using Regular Expressions is not the best idea, starting from JMeter version 3.0 you have JSON Extractor which allows executing arbitrary JSON Path queries against JSON responses.
In particular your case the relevant JSON Path query should look like:
$..name
Demo:

Jmeter : How to extract first element from json array

I am trying to extract first element from a json array. Below mentioned is json array
[
{
"cohortDefinition": {
"Key": 1151,
"id": 1798,
"srcId": "3526",
"pcKey": -1,
"userName": "CHROME_USER",
"name": "JMeter2017-01-06-1483749546167",
"Type": "SUBJECT",
"tool": "CB",
"count": 32757,
"extractionStatus": "",
"dateCreated": "2017-05-10T17:48:45Z"
},
"datasource": {
"id": 2,
"name": "health",
"subjectCount": 116352
},
"project": {
"id": 747,
"name": "Jmeter Project"
}
},
{
"cohortDefinition": {
"Key": 1150,
"id": 1796,
"srcId": "3525",
"pcKey": -1,
"userName": "CHROME_USER",
"name": "JMeter2016-10-27-1477620919644",
"Type": "SUBJECT",
"tool": "CB",
"count": 32757,
"extractionStatus": "",
"dateCreated": "2017-05-10T16:57:11Z"
},
"datasource": {
"id": 2,
"name": "health",
"subjectCount": 116352
},
"project": {
"id": 747,
"name": "Jmeter Project"
}
}
]
From above json i would like to extract first value ie. srcId": "3526".
I tried doing following expression in Jmeter extractor
$..cohortDefinition.srcId[1]
However it is not working. If anyone know how to do this please do let me know.
After JMeter 3.0, you can use JSON Extractor, see:
https://stackoverflow.com/a/47043204/460802
Before JMeter 3.0:
Please follow the below steps to retrieve srcId.
Add a JSON Path Extractor to your request and configure below values.
Destination Variable Name - myVar
JSON Path Expression - $..cohortDefinition.srcId - this will extract all the srcIDs from the JSON.
Default Value - Not Found or Err
Add a Debug Sampler and View Results Tree to your test plan.
Save it and execute.
In Debug Sampler, you can view all the srcId as shown below.
You can now use myVar_1 and myVar_2 in your test plan
using ${myVar_1} ${myVar_2}
No need for Plugin, JMeter has a JSON Extractor that will provide this feature:
Notice:
JSON Path Expression is: $..cohortDefinition.srcId
Match No : 1

Resources