Jmeter extracting values from response and sending to other requests - jmeter

I have a JSON response below:
"books": [
{
"name" : "test1",
"id" : "T01"
},
{
"name" : "test2",
"id" : "T02"
},
{
"name" : "test3",
"id" : "T03"
},
]
I am extracting all respective ids and sending it as a body to another request.
Other request takes it as an array of strings but when I am extracting it, it is showing as integers:
Currently it shows: ids_ALL = [T01, T02, T03]
and I have to pass it like: ids_ALL = ["T01", "T02", "T03"]
Note: I am suffixing _ALL to get all ids.
Since it is not passing the array as string, I am getting an error.
Is there away to extract it and put it in array of strings or way to use post-processer and then convert the array and send to other request.

This one-liner will extract all the IDs and generate the JSON Array you're looking for:
vars.put('payload', (new groovy.json.JsonBuilder(new groovy.json.JsonSlurper().parse(prev.getResponseData()).books.id.collect()).toPrettyString()))
no other extractors are needed, you can refer the generated array as ${payload} later on where required
In Taurus it can be put into the JSR223 Block
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

You can use JSON Extractor or JSON JMESPath Extractor to extract all the ids from the response.
Place a JSR223 Post processor just below the JSON Path Extractor to create a list of Strings (ids)
def idCount=vars.get("booksIds_matchNr").toInteger()
def lstIds=[]
for(i in 1..idCount){
String currentId=vars.get("booksIds_" + i)
lstIds.add("\""+ currentId + "\"" )
//lstIds.add("${currentId}" )
}
vars.putObject("lstIds",lstIds)
You can access the list with vars.getObject("lstIds")
List of strings could be seen in the view result tree.

Another quick solution
Add a JSR223 Post Processor below the JSON Extractor to create strings with the available values.
String booksIds_ALL=vars.get("booksIds_ALL")
def lstIds = "\"" + booksIds_ALL.replace(",", "\",\"") + "\""
vars.putObject("lstIds",lstIds)

Related

Jmeter - Extracting a a JSON value by validating with another value

It is possible to get a JSON value by validating with another value.
For example i am getting multiple responses as below;
{
"place_name": "Home",
"selected_icon": "http:\/\/abc.tech\/images\/home_selected.png",
"updated_at": "2017-11-10 12:12:34.795339",
"icon": "http:\/\/abctech\/images\/home.png",
"created_at": "2017-11-10 12:12:34.795339",
"active_flag": 1,
"id": 1
}
I have extract the "place name" using $..place_name. Now validating the place_name i need to extract the icon. Which means i need to save the icon value which matches the "place name" only
Varibale : new
JSON extractor 1 : $..place_name (second value so only using $..)
Varibale : abc
JSON extractor 2:$..[?(#.place_name==${new})].icon
Matc No (o for random): -1
The value is not passing any suggestion
i have tried with this extarctor also JSON extractor 2:$..[?(#.place_name=='${new}')].icon
Match random i am taking as -1 because i am passing the value in for each controller
I don't find any issue with you approach, it is working fine. I have tried with your payload and changed your payload with multiple value. Both are working fine.
With your payload where single place_name is there,
With multiple value for icon:
Passed the value to for each controller

Obtain index of random element from json array in JMeter Json Path Post Processor

I have strange case for jmeter. Imagine that we have an json array with elements like this:
{
"id" : 123456,
"name": "TEST"
}
So I want to get random element from array that has id. For this case I use Json Path PostProcessor with expression like this $.elements[?(#.id)]
But for some reasons I need an index of this element. So I can create BeanShellPostProcessor generate random index and then use same Json Path PostProcessor with expression like this $.elements[${PARAM_ElementIndex}].
But in some cases this array can be empty and Json Path PostProcessor wil fail with exception like this:
jmeter.extractor.json.jsonpath.JSONPostProcessor: Error processing JSON content in PARAM_ResumeId, message:No results for path: $['elements'][0]['id']
So may be someone can suggest any solution
I would recommend use Groovy instead of Beanshell as:
Well-behaved Groovy scripts can be compiled into bytecode therefore performance will be much higher
Groovy has built-in JSON support
So given you have JSON Response like:
{
"elements": [
{
"id": 123456,
"name": "TEST"
},
{
"id": 7890,
"name": "TEST2"
}
]
}
You can extract random ID along with its index using the following example Groovy code in the JSR223 PostProcessor:
import groovy.json.JsonSlurper
import java.util.concurrent.ThreadLocalRandom
String response = prev.getResponseDataAsString()
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(response)
int size = json.elements.size
if (size > 0){
def randomIndex = ThreadLocalRandom.current().nextInt(size)
def value = json.elements.get(randomIndex).id
log.info('Index: ' + randomIndex)
log.info('Value: ' + value)
}
Demo:
References:
Parsing and producing JSON
Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!

How to access nested array values in JSON response and use them in a loop in next service in JMeter

I am working on a POST service that give JSON response.
I have to extract certain value from the JSON response. example-
`{
"Result":
{ "Id":22
"StartTime":
"EndTime":
"RoutePoints":
[{ "Id":675,
}
{ "Id":676,
}
]
}
} `
My first part of the question-
How do I refer the "Id" variable inside the "RoutePoint" array using regular expression extractor? I can simply use "Id", but I also have an "Id" variable outside the "RoutePoint" array.
Secondly-
How do I take the "Id" each time and run them in a loop in the following service? Example- I take "Id=675" and perform a job, then take "Id=676" and perform that same job. Please be as detailed as possible, I am new to JMeter.
I would recommend going for JSON Path PostProcessor which is available since JMeter 3.0
Add JSON Path PostProcessor as a child of the request which returns above JSON and configure it as follows:
Variable Names: anything meaningful, i.e. Id
JSON Path Expressions: $..RoutePoints.*.Id
Match Numbers: -1
You should get variables like:
Id_1=675
Id_2=676
Id_matchNr=2
suitable for iteration with i.e. ForEach Controller
Demo:
References:
JSONPath - XPath for JSON
Advanced Usage of the JSON Path Extractor in JMeter

Looping through JSON response + in JMETER

I am using Jmeter for performance testing and stuck at following point:
I am getting a JSON response from Webapi as follows:
PersonInfoList:
Person
[0]
{
id: 1
name: Steve
}
[1]
Person
{
id: 2
name: Mark
}
I need to get the ids based on the count of this JSON array and create a comma separated string as ("Expected value" = 1,2)
I know how to read a particular element using JSON Post processor or Regex processor but am unable to loop through the array and create a string as explained so that I can use this value in my next sampler request.
Please help me out with this: I am using Jmeter 3.0 and if this could be achieved without using external third party libs that would be great. Sorry for the JSON syntax above
Actually similar functionality comes with JSON Path PostProcessor which appeared in JMeter 3.0. In order to get all the values in a single variable configure JSON Path PostProcessor as follows:
Variable Names: anything meaningful, i.e. id
JSON Path Expressions: $..id or whatever you use to extract the ids
Match Numbers: -1
Compute concatenation var (suffix _ALL): check
As a result you'll get id_ALL variable which will contain all JSON Path expression matches (comma-separated)
More "universal" answer which will be applicable for any other extractor types and in fact will allow to concatenate any arbitrary JMeter Variables is using scripting (besides if you need this "expected value and parentheses)
In order to concatenate all variables which names start with "id" into a single string add Beanshell PostProcessor somewhere after JSON Path PostProcessor and put the following code into "Script" area
StringBuilder result = new StringBuilder();
result.append("(\"Expected value\" = ");
Iterator iterator = vars.getIterator();
while (iterator.hasNext()) {
Map.Entry e = (Map.Entry) iterator.next();
if (e.getKey().matches("id_(\\d+)")) {
result.append(e.getValue());
result.append(",");
}
}
result.append(")");
vars.put("expected_value", result.toString());
Above code will store the resulting string into ${expected value} JMeter Variable. See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information regarding bypassing JMeter limitations using scripting and using JMeter and Java API from Beanshell test elements.
Demo:

Parse an array from the response message using JMeter

I am a beginner with Jmeter tool.
I use JMeter to hit a URL and obtained a response message in JSON format.
I want to parse the response to extract every array element and use it to append to the tail of another URL. Can you please tell me how to do this?
example: { "reply": { "code": "111", "status": "SUCCESS", "customer": [ "222-a", "b-333", "44-4", "s-555", "666", "777", "88-8" ] } }
You can use a Regular Expression Extractor (as child of your URL sampler) to first extract the array of values:
Reference name: ary
Regular Expression: \[([^\]]+)\]
Template: $1$
Match No: 1
then use another Regular Expression Extractor to extract the values:
Apply To JMeter variable: ary
Reference name: vals
Regular Expression: "([^"]+)"
Template: $1$
Match No: -1
The match no -1 creates variables vals_1 .. vals_7, which you can then use in a ForEach Controller to assign to a JMeter variable:
Input variable prefix: vals
Output variable name: id
[v] Add '_' before number?
now you can use the JMeter variable ${id} in a nested URL sampler to pass the customer id in a URL.

Resources