JMeter: How to count JSON objects in an array using jsonpath

In JMeter, I want to check the number of objects in the JSON array that I get from the server.

For example, for a specific request, I expect an array with 5 objects.

[{...}, {...}, {...}, {...}, {...}]

After reading this: count participants using jsonpath? I tried using the following JSON Path Assertion:

  • JSON path: $
  • Expected Value: hasSize (5)
  • Confirmation from expected value = verified

However, this does not work properly. When I actually get 5 objects in the array, the statement of the answer says that it does not match.

What am I doing wrong? Or how else can I do this?

+6
source share
2 answers

Although JSONPath Extractor does not provide a hasSize function hasSize it can still be done.

Given the JSON example from the PMD UBIK-INGENIERIE answer, you can get the number of matches in the book array in at least two ways:

1. The easiest (but fragile) way is to use the Regular Expression Extractor .

As you can see, in category 4 entries:

 { "category": "reference", { \"category\": \"fiction\" ... 

If you added a regular expression extractor configured as follows:

JSON Regex

It will capture all category entries and return the number of matches, as shown below:

JSON Regex Matches

This way you can use this variable ${matches_matchNr} wherever you need it.

This approach is simple and easy to implement, but it is very vulnerable to any changes in the response format. If you expect JSON data to change in the foreseeable future, continue to the next option.

2. A more complicated (but more stable) way is to call JsonPath methods from Beanshell PostProcessor

JMeter has a Beanshell scripting extension mechanism that has access to all variables / properties in scope, as well as to the base dependencies of JMeter and third-party dependencies. In this case, you can call the JsonPath library (which is located under the hood of the JsonPath Extractor) directly from Beanshell PostProcessor.

 import com.jayway.jsonpath.Criteria; import com.jayway.jsonpath.Filter; import com.jayway.jsonpath.JsonPath; Object json = new String(data); List categories = new ArrayList(); categories.add("fiction"); categories.add("reference"); Filter filter = Filter.filter(Criteria.where("category").in(categories)); List books = JsonPath.read(json, "$.store.book[?]", new Filter[] {filter}); vars.put("JSON_ARRAY_SIZE", String.valueOf(books.size())); 

The above code evaluates the JSONPath $.store.book[?] response of the parent sampler, counts the number of matches and stores it in the ${JSON_ARRAY_SIZE} JMeter variable.

JSON Beanshell Variable

which can subsequently be reused in an if clause or statement.

Recommendations:

+10
source

This is not possible with the plugin you are using (JMeter-plugins).

But this can be done with JSON Extractor starting from JMeter 3.0, this plugin was donated by UbikLoadPack ( http://jmeter.apache.org/changes_history.html )

Example:

Let's say you have this JSON that contains an array of books:

  { "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}, { "category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99}, { "category": "fiction","author": "JRR Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99} ], "bicycle": {"color": "red","price": 19.95}} } 

To have this account:

1 / Add JSON Extractor:

JSON Extractor configuration

Then the bookTitle_matchNr account will be available, which you can access through:

$ {BookTitle_matchNr}

Running this test plan will display this:

Run result

As you can see, Debug Sampler- $ {bookTitle_matchNr} shows Debug Sampler- 4

-2
source

Source: https://habr.com/ru/post/974417/


All Articles