Looping through multiple Regex extractor output - jmeter

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.

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

Trying to get a JSONPath array of all leaves on json object

New to Go. My first project is to compare a NodeJS proxy and a Go proxy for account number tokenization. I have been doing NodeJS for a few years and am very comfortable with it. My proxies will not know the format of any request or response from the target servers. But it does have configurations coming from Redis/MongoDB that is similar to JSONPath expression. These configurations can change things like the target server/path, query parameters, headers, request body and response body.
For NodeJS, I am using deepdash's paths function to get an array of all the leaves in a JSON object in JSONPath format. I am using this array and RegEx to find my matching paths that I need to process from any request or response body. So far, it looks like I will be using gjson for my JSONPath needs, but it does not have anything for the paths command I was using in deepdash.
Will I need to create a recursive function to build this JSONPath array myself, or does anyone know of a library that will produce something similar?
for example:
{
"response": {
"results": [
{
"acctNum": 1234,
"someData": "something"
},
{
"acctNum": 5678,
"someData": "something2"
}
]
}
}
I will get an array back in the format:
[
"response.results[0].acctNum",
"response.results[0].someData",
"response.results[1].acctNum",
"response.results[1].someData"
]
and I can then use my filter of response.results[*].acctNum which translates to response\.results\[.*\]\.acctNum in Regex to get my acctNum fields.
From this filtered array, I should be able to use gjson to get the actual value, process it and then set the new value (I am using lodash in NodeJS)
There are a number of JSONPath implementations in GoLang. And I cannot really give a recommendation in this respect.
However, I think all you need is this basic path: $..*
It should return in pretty much any implementation that is able to return pathes instead of values:
[
"$['response']",
"$['response']['results']",
"$['response']['results'][0]",
"$['response']['results'][1]",
"$['response']['results'][0]['acctNum']",
"$['response']['results'][0]['someData']",
"$['response']['results'][1]['acctNum']",
"$['response']['results'][1]['someData']"
]
If I understand correctly this should still work using your approach filtering using RegEx.
Go SONPath implementations:
http://github.com-PaesslerAG-jsonpath
http://github.com-bhmj-jsonslice
http://github.com-ohler55-ojg
http://github.com-oliveagle-jsonpath
http://github.com-spyzhov-ajson
http://github.com-vmware-labs-yaml-jsonpath

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 assert a JSON response which have results in random order every time in JMeter?

I am using JSON Assertion to assert if a JSON path exists. Suppose I have a JSON response of an array of 'rooms' that 'contains' an array of cabinets, just like the following example
"rooms":
[
{
"cabinets":
[
{
"id":"HFXXXX",
"locationid":null,
"name":"HFXXXX",
"type":"Hosp"
},
{
"id":"HFYYYY",
"locationid":null,
"name":"HFYYYY",
"type":"Hosp"
},
{
"id":"HFZZZZ",
"locationid":null,
"name":"HFZZZZ",
"type":"Hosp"
}
],
"hasMap":false,
"id":"2",
"map":
{
"h":null,
"w":null,
"x":null,
"y":null
},
"name":"Fantastic Room#3"
}
],
[
{ "cabinets":
[
{
"id":"HFBBBB",
"locationid":null,
"name":"HFBBBB",
"type":"Hosp"
}
],
"hasMap":false,
"id":"3",
"map":
{
"h":null,
"w":null,
"x":null,
"y":null
},
"name":"BallRoom #4"
}
]
I want to Make sure that the 'id' of all the cabinets are correct, therefore I define the JSON path as rooms[*].cabinets[*].id and expect the value to be ["HFXXXX","HFYYYY","HFZZZZ","HFBBBB"]
This works perfectly except that sometimes the values are returned in a different order["HFBBBB", "HFXXX","HFYYYY","HFZZZZ"] instead of ["HFXXXX","HFYYYY","HFZZZZ","HFBBBB"], hence the assertion will fail. The problem is with the order of the returned array and not the values themselves.
Is there a way to sort the order of a response before Asserting and keep using the JSON assertion? or the only way of doing this is extracting the value i want to assert against and use it in JSR223 Assertion (groovy or javascript)?
if that is the case can you show me an example of how I could do it in JSR223 plugin.
I would recommend using a dedicated library, for instance JSONAssert, this way you will not have to reinvent the wheel and can compare 2 JSON objects in a single line of code
Download jsonassert-x.x.x.jar and put it somewhere to JMeter Classpath
Download suitable version of JSON in Java library and put it to JMeter Classpath as well. If you're uncertain regarding what is "JMeter Classpath" just drop the .jars to "lib" folder of your JMeter installation
Restart JMeter so it would be able to load the new libraries
Add JSR223 Assertion as a child of the request which returns the above JSON
Put the following code into "Script" area:
def expected = vars.get('expected')
def actual = prev.getResponseDataAsString()
org.skyscreamer.jsonassert.JSONAssert.assertEquals(expected, actual, false)
It will compare the response of the parent sampler with the contents of ${expected} JMeter Variable, the order of elements, presence of new lines, formatting do not matter, it compares only keys and values
In case of mismatch you will have the error message stating that as the Assertion Result and the full debugging output will be available in STDOUT (console where you started JMeter from)

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

Resources