I am trying to obtain data from sent JSON and use it further in another request.
My sent JSON also has dynamic variables like ${data} so the trick is that it has to execute first in order to be able to extract.
Let's say I have the following SENT JSON:
{
"field_one": ${data1},
"field_three": [more data],
"field_two": ${data2}
}
Question is: How can I extract "field_one" and "field_two" values from the sent request?
Thanks
You don't need to extract them, they are ${data1} and ${data2} so you can re-use these JMeter Variables anywhere in the script.
If I don't understand something obvious or you need to copy the values to another JMeter Variables, you can extract them as follows:
Add JSR223 PostProcessor as a child of the request which sends above JSON
Put the following code into "Script" area:
def requestBody = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
vars.put('field_one', requestBody.field_one)
vars.put('field_two', requestBody.field_two)
That's it, now you should have ${field_one} and ${field_two} JMeter Variables holding the values you're looking for.
In the above example sampler stands for HTTPSamplerProxy and vars for JMeterVariables, check out Top 8 JMeter Java Classes You Should Be Using with Groovy for details on the above and other JMeter API shorthands available for JSR223 Test Elements.
More informaion:
JsonSlurper
Apache Groovy: Parsing and producing JSON
I will get json response from last request, then I will parse the response and get a variable array, then create new request base on each element in that array one by one. I don't know how to implement it.
Use JSON Extractor and ForEach Controller combination. The idea is to have variables like:
var_1=foo
var_2=bar
var_3=baz
So you would be able to iterate them using foreach loop. See Using Regular Expressions in JMeter article to get the overall idea.
Steps to follow:
Add JSON Extractor (>= 3.0 version) Or JSON Path Extractor (< 3.0 version) plugin, to the HTTP Request sampler, as a child, in which JSON response is received.
Add the JSON Path Expressions to capture the specific values and store it in variable names, say capturedArray. refer JSON Path Syntax.
In later requests, i.e., HTTP Request Samplers, you can retrieve the array value by using the syntax ${capturedArray}
suppose I have a webservice that generate random password and return json response, this password is used in the following request. is there anyway to do this in jmeter, i.e to extract the password from the first response and use it in the next request
We have Regualr Expression Extractor Or JSON Path Extractor in JMeter to extract the values from 1 response & use it in subsequent requests.
As Vinoth S said, JSON extractor is the way to go. You can download it from here. Document how to use it is here. Just put it in request from which you want to extract, type vairable_name that you will use in the next request as ${variable_name}, JSON Path is quite easy to set, probably, if the response is not to complicateg just the property name from JSON will be enough, or something like $.varName and you're ready to go.
I have fetched multiple paths of multiple urls from the JSON response using JSON Path extractor in jmeter. The paths are stored in the jmeter variables like url_0,url_1, url_2 and so on. The server IP address is constant.
These urls fetched from the JSON response have to be invoked after parsing the JSON response as stated earlier. How do I achieve this particular behaviour in jmeter?
Use ForEach Controller as it described in Using Regular Expressions in JMeter
If you have variables like:
url_0=/some/path
url_1=/some/other/path
url_2=/some/another/path
etc.
Configure ForEach Controller as follows:
Input Variable Prefix: url
Start index for loop: -1
Output variable name: path
Add "_" before number: check
And use ${path} variable in underlying HTTP Request sampler
Scenario :-
Im performing Load testing using API's
HTTP Request 1
I logged in using http://cabhound.com:1000/v2/driver/login and I got the below response
{"statusCode":200,"statusMessage":"Success","errorMessage":"","responseData":{"id":0,"userName":"PQeurentraps5S#tarento.com","firstName":"Partner","lastName":"Tarento","phoneNumber":"2641148625","email":"tamvrentrapnsr#tarento.com","password":"","addressOne":"","addressTwo":"","city":"","state":"","zipCode":"","loginCount":156,"welcome":"","smsOptIn":false,"promoCode":"","userNotification":"","errorMessage":"","message":"","sessionId":"6063tnerLt3013951671120oDse18492930#2","osType":"","osVersion":"","deviceType":"","deviceId":"","latitude":"","longitude":"","timeZone":"","appVersion":"","company":"Tarento","licenceNumber":"","vehicleType":"","vehicleFeature":null,"subscriptionType":"unlimited","driverWorkingCity":"Bangalore","vehicleNumber":"","locationUpdateTime":20,"rate":0,"reliable":0,"distance":0.0,"eta":0,"latitudeLongitude":"","status":"ON","payment":{"paymentType":"","cardNumber":"","cvnNumber":"","expDate":""},"vehicleTypeList":["Sedan","Limousine","SUV/Wagon","Minivan","Other"],"vehicleFeatureList":["Navigation System","Eco Friendly","Handicap accessible","Accepts credit cards"],"driverId":582,"currentLocation":null,"companyCode":"tarento","acceptanceRate":0,"like":0,"profileIndicator":0,"payWithCabHound":false,"smsSupport":false,"paymentInfo":false,"geoInfo":"","active":true}}
Please see the session id in the above response,which I want to use in next http request data
HTTP Request 2
http://cabhound.com:1000/v2/driver/dutyStatus
Below is the data which I need to post,here I want to use session id of HTTP Request 1
{"status":"ON","sessionId":"1311tnerLt9013956793297oDse462783#2","longitude":"77.686700","userName":"erpkrentrapJps#tarento.com","latitude":"12.934487"}
How to pass the session id of HTTP Request 1 (response) to HTTP Request 2 Post Data
Help me in this which I have strucked
I would recommend using JSON Path Extractor available through JMeter Plugin (you'll need Extras with Libs Set.
Regex are headache to develop, especially for JSON data which can be multiline. Beanshell has known performance issues, so using a plugin is the best option.
Relevant JSON Path query for your sessionId will look as:
$.responseData.sessionId
See Parsing JSON section of Using the XPath Extractor in JMeter guide for more details and XPath to JSON Path mapping
I can see 2 solutions for above problem,
Use Regular expression extractor to extract the value (I haven't used it with json response but I think it will work)
Use Beanshell preprocessor or postprocessor in which you can get response and find required sessionID using substr or json parsers or use simple java code. Extract the required value and use it in next request.