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

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

Related

Jmeter extracting values from response and sending to other requests

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)

I have a dynamic value in response but it keep on changing the position. How can i capture it

I have a dynamic value in response but it keep on changing the position. How can i capture it.
ex:1st iteration value is 2nd position
2nd iteration value is 4th position
3rd iteration value is 1st position...like that
Can you some one please guide me how to capture this value using Regular Expression Extractor or any other Extractor.
Perhaps Regular Expression Extractor is not the best choice, especially when it comes to HTML response type so make sure to use the suitable Post-Processor, i.e.
HTML - CSS Selector Extractor
XML - XPath Extractor
JSON - JSON Extractor
Anything else: Boundary Extractor might be easier to use than the Regular Expression Extractor
Going forward include (at least partial) response in your question and indicate which value you want to extract so we could come up with the most efficient approach.
As of JMeter 3.0, it’s far easier to extract data from JSON responses using the JSON variable extractor. JSON is an extremely simple data format which has taken over XML a few years ago.
An increasing number of REST APIs and servers, are using JSON as their primary data exchange format. Here, we will use JMeter to parse the JSON response.
Suppose we have a JSON response as follows:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
To parse the above JSON with JMeter, we need to add the JSON Extractor to our test plan.
Right click on Test Plan –> Add –> Post Processors –> JSON Extractor
jmeter json extractor parser
Now, we should see the following view:
json extractor jmeter
In the JSON Path expressions field, we can insert our JSON path to parse the JSON response
Here are some example Json Path expressions that can be used to extract data from the Json document exposed above:
JSONPATH RESULT
$.store.book[*].author The authors of all books
$..author All authors
$.store.* All things, both books and bicycles
$.store..price The price of everything
$..book[0,1] The first two books
$..book[:2] All books from index 0 (inclusive) until index 2 (exclusive)
$..book[2:] Book number two from tail
$..book[?(#.isbn)] All books with an ISBN number
$.store.book[?(#.price < 10)] All books in store cheaper than 10
$..book[?(#.price <= $[‘expensive’])] All books in store that are not “expensive”
$..book[?(#.author =~ /.*REES/i)] All books matching regex (ignore case)
$..* Give me every thing
$..book.length() The number of books

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 multiple Regex extractor output

Can you please tell me how to loop through the result of a Regex Post Processor that returns multiple values?
Example:
JSON Response message:
{
"reply": {
"code": "111",
"status": "SUCCESS",
"customerID": [
"222-a",
"b-333",
"44-4",
"s-555",
"666",
"777",
"88-8"
]
}
}
Regx extractor helped me extract each individual component of the array:
links_1=222-a
links_2=b-333
I can use some.url/${links_1}.
Here is exactly what I am trying to achieve, but this does not seem to work.
Can you please help me?
Loop through the Regex extracted individual variable using a counter and append each one in another HTTP request sampler:
WhileController(${__javaScript(${C} < ${links_matchNr})})
HTTPSampler use ${__V(links_${C})}
Counter (start=1,increment=1,maximum=${links_matchNr},referenceName=C)
Use ForEach Controller:
input variable : links
output variable : link for example
You can then use each value inside Controller through:
${link}
I have created this tutorial, http://goo.gl/4cBno I hope its useful. To view the desktop sharing clearly, click on the full screen icon at the bottom right.

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