0
votes

I am having sample json

[
:   {
:   :   "id":"255",
:   :   "name”:”abc”,
:   },
:   {
:   :   "id":"257",
:   :   "name”:”xyz”,
:   }
]

I am using json path extractor in jmeter to get the values of id and name. However is it possible to get the count of ids or names in a straight forward way. I know if I use regex extractor and using for loop to get the count.

3

3 Answers

2
votes

Use regular expression extractor and set no. of match to a negative no. then reference name_matchNr variable should give you the count of occurances of string.

like,

enter image description here

Then expression_matchNr variable should give you the count of id occurances.

For additional reference see JMeter help,Regular Expression extractor

0
votes

It is possible via some scripting.

For example, if you have the following JSON payload:

{"employees":[
    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

And the following JSONPath Expression:

$.employees..firstName

which returns first names as follows:

["John","Anna","Peter"]

You can get matches number by counting commas in the resulting variable.

Given that you're using "name" as the Variable Name in JSON Path Extractor

  1. Add the a Beanshell PostProcessor below JSONPath extractor
  2. Put the following code into the PostProcessor's "Script" area:

    int matches = vars.get("name").split(",").length;
    vars.put("matches",String.valueOf(matches));
    
  3. Optionally add a Debug Sampler and a View Results Tree Listener and run your test

You should have "matches" variable containing "3"

For more information on Beanshell scripting in Apache JMeter see How to use BeanShell: JMeter's favorite built-in component guide.

0
votes

There is a way to do it using Json Extractor as well. For your given Json you can add a Json Extractor in your HTTP Sampler and a Debug Sampler(to see result) next to you HTTP sampler as shown below enter image description here

and set variable and path expression as follow enter image description here

See response data of debug sampler and you will get result at last

idCount_matchNr=2