Jmeter: Json Extractor: Response from first request to be used in next Request - jmeter

I have a Http request for which i am getting data from CSV data file and i am getting a response body as {"abc":"123456123"}. I have added JSON extractor as child to this Http request. I am given variable name as abc and tried json path as both $..abc or $.abc and now i have added new Http request(2) in which i want to pass variable name from json extractor in request body(2). I have used same variable name in the http request body(2) but the variable name is not passing in the new request.
I gave all the options in json path in json extractor, i am expecting that for each response {"abc":"123456123"}(this value will change for each request) json extractor should get the response value and pass in the next request body.
When i am running the script, the value is not passed in request2.
Response of request 1 :- for single thread
{"abc":"123456123"}
Json Extractor:-
name of variable : abc
JSON path:- $.abc
Request 2:-
{
"a":{
"b":[
{
"mRID":"${abc}",
"abc":[
{
"a":[
{
"n":[
{
"na":[
{
"a":"",
"a":"AF",
"value":"add"
}
]
}
],
"cr":[
{
"name":"0071",
"nameType":"r#"
},
{
"name":89923,
"nameType":"NR#"
}
],
"CS":[
{
"dateTime":"${__timeShift(yyyy-MM-dd'T'hh:mm:ss)}",
"type":"CS",
"value":"x"
}
]
}
]
}
]
}
]
}
}

I cannot reproduce your issue using Dummy Sampler and the JSON Extractor configuration you described.
Try adding Debug Sampler after the uuu request and see if there is abc variable generated and what value does it have.
Also check jmeter.log file for any suspicious entries

Related

extract http request response and proceed to next http request by using while controller

I want to use while controller to keep sending GET HTTP Request and only proceed to next POST HTTP Request when the GET response "model_name": "Model-Test-20220221-0001"
POST HTTP Request - paramter model_name = Model-Test-${variable}-${counter}
While Controller
GET HTTP Request. sample GET response at below
Json Extractor
POST HTTP Request only when While Controller found the exact model_name.
JSON Extractor:
Names of created variable: modelname
Json path expressions: I have tried this but failed
$..valid_model_list[?(#.model_name = ${Model-Test-${variable}-${counter}})]
This is sample HTTP Request response:
{
"message": "success",
"valid_model_list": [
{
"meta_data": {
"corpus_list": [
"test1"
],
"id": "0c36effa244b4f6596d10f9e675303e1",
"sample_rate": 16000,
"split_ratio": {
"test": 40,
"train": 60
},
"model_name": "Model-Test-20220221-0001",
"status": "ok"
},
{
"meta_data": {
"corpus_list": [
"test1"
],
"id": "0c36effa244b4f6596d10f9e675303e1",
"sample_rate": 16000,
"split_ratio": {
"test": 40,
"train": 60
},
"model_name": "Model-Test-20220221-0002",
"status": "ok"
}
}
]
}
}
We're "unsure" as well as we don't know what you're trying to achieve.
JSON Extractor can extract values from the response, it's not possible to use it to extract values from the request, moreover you should "know" what you're sending in the request and extracting values from the response is the essential part of the correlation process
If you're looking for a JSONPath expression extracting the model_name attribute value you're supposed to provide full JSON. There is a Deep Scan operator so you should be able to use something like:
$..model_name
but if there are more than 1 matches you might need to limit the output with Filter Operators
More information:
JsonPath Getting Started
API Testing With JMeter and the JSON Extractor

How to use array from response1 in request2 in jmeter?

I am trying to extract a jsonArray from response of request1 and use it in request2. I am using JSONextractor the steps in this question but I am getting the array as different variables instead of 1 jsonArray.
My JsonExtractor:
Output of debug sampler:
Request:
{
"items": [
{
"id": "asd"
},
{
"id": "def"
},
{
"id": "hij"
}]
}
I don't know what I'm doing wrong that is extracting values in different variables instead of 1 jsonArray.
You can generate a JSON request body from the JMeter Variables which are coming from the JSON Extractor using JSR223 PreProcessor
Add JSR223 PreProcessor as a child of the request which you want to parameterize
Put the following code into "Script" area:
def payload = [:]
def items = []
1.upto(vars.get('userIds_matchNr') as int, { index ->
items.add([id: vars.get('userIds_' + index)])
})
payload.put('items', items)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
You should be able to put the ${payload} JMeter Variable reference into the request "Body Data" tab
Demo:
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Which controller to use in Jmeter?

I want to create a controller that should run a till the condition fails. How it can be implemented in Jmeter.
The controller should contain a HTTP Request with a post body which is dynamic the request should continue till the condition fails,but i dont know where the should i apply that condition.
{
"access": {
"identifier": "9876f",
"Reproduce": "Right",
"possible": {
"id": "u7ur038",
"value": "Move"
}
}
}
If the response "Reproduce" contain "Right"then it should run again HTTP Request for new body and If the response "Reproduce" contain "Wrong"then it should stop executing.
You can use a While Controller which will contain your request.
Condition of While Controller will be:
${__jexl3("${response}" != "Wrong")}
Add as child of your HTTP Request a JSON Extractor:
Names of created variables: response
JSON Path Expressions: $..Reproduce
Match No.: 1
To reset variable for next thread loop iteration, add before While Controller a Flow Control Action and put inside it a preprocessor called User Parameters .
Click « Add Variable » and set :
Name: response
User_1: Right

How to get Number of rows in Jmeter using Json Path Extractor

My Response is:
{
rows{
["2","xxx","yyyy"],
["3","xxx","yyyy"],
["4","xxx","yyyy"],
}
}
I am using $.rows to get all the rows. as well I am giving $.rows[0].[1] to get the value from 1st row and 1st value..
I am trying to get the total number of rows using $.rows.size() or length. It is not following exception. How to get the number of rows?
Exception: Options AS_PATH_LIST and ALWAYS_RETURN_LIST are not allowed
when using path functions!
Given the following JSON:
{
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}
If I add JSON Path Extractor as a child of the sample which returns the above JSON and configure it like:
Reference Name: arraySize
JSONPath Expression: $..cars.length()
I am able to see the following variables in the View Results Tree listener:
arraySize=[3]
arraySize_1=3
arraySize_matchNr=1
Which seems to be something you're looking for.
You can install JSON Path Extractor along with JSON Path Assertion using JMeter Plugins Manager

JMeter Modify the GET response to use in PUT

I have two requests, the first one is GET, second is PUT. I should receive the response data from GET request, modify it a little bit and send with PUT request. So far I managed to do all, except modifying the response data.
For GET request I use Regular Expression Extractor as Preprocessor:
And currently I send the PUT request without modifying the data:
JSON structure:
{
"property1" : 1,
"property2" : "2",
"innerPropery" : {
"innerProperty1" : "value1",
"innerProperty2" : "value2",
"innerProperty3" : "value3"
}
}
I should change the innerProperty2.
Thanks!
You can do it without Regular Expression Extractor interim step.
Add JSR223 PostProcessor as a child of the getForm request
Choose groovy in the "Language" drop-down
Put the following code into "Script" area:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def response = prev.getResponseDataAsString()
def json = new JsonSlurper().parseText(response)
def builder = new JsonBuilder(json)
builder.content.property2 = '2.1'
vars.put("response", builder.toPrettyString())
In saveForm request use ${response} as the request body
References:
Parsing and Producing JSON
Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!
I have managed to do this via BeanShell PostProcessor. First I have added a new BeanShell PostProcessor: click right button on getFrom, Add -> Post Processors -> BeanShell PostProcessor. After that I opened the reated BeanSChell and have written the following code in Script field:
responseString = vars.get("response");
log.info("Received response: " + responseString);
responseString = responseString.replace("\"prop1\" : \"value2\"", "\"prop1\" : \"value2.1\"");
log.info("Response to send: " + responseString);
vars.put("modifiedResponse", responseString);
In Parameters field I have written:
response
Finally, I changed for PUT request the body from
${response}
to
${modifiedResponse}

Resources