8

In JMeter I want to check the number of objects in a JSON array, which I receive from the server.

For example, on a certain request I expect an array with 5 objects.

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

After reading this: count members with jsonpath?, I tried using the following JSON Path Assertion:

  • JSON Path: $
  • Expected value: hasSize(5)
  • Validate against expected value = checked

However, this doesn't seem to work properly. When I actually do receive 5 objects in the array, the response assertion says it doesn't match.

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

1

2 Answers 2

13

Although JSONPath Extractor doesn't provide hasSize function it still can be done.

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

1. Easiest (but fragile) way - using Regular Expression Extractor.

As you can see, there are 4 entries for category like:

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

If you add a Regular Expression Extractor configured as follows:

JSON Regex

It'll capture all the category entries and return matches number as below:

JSON Regex Matches

So you will be able to use this ${matches_matchNr} variable wherever required.

This approach is straightforward and easy to implement but it's very vulnerable to any changes in the response format. If you expect that JSON data may change in the foreseeable future continue with the next option.

2. Harder (but more stable) way - calling JsonPath methods from Beanshell PostProcessor

JMeter has a Beanshell scripting extension mechanism which has access to all variables/properties in scope as well as to the underlying JMeter and 3rd-party dependencies APIs. In this case you can call JsonPath library (which is under the hood of 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 code above evaluates JSONPath expression of $.store.book[?] against parent sampler response, counts matches number and stores it into ${JSON_ARRAY_SIZE} JMeter Variable

JSON Beanshell Variable

which can later be reused in an if clause or an assertion.

References:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for these suggestions! For now I'll use the quick solution with the Regular Expression Extractor, it's sufficient. But I'll probably use the BeanShell PostProcessor later. great :-)
Prefer the JSR223 + Groovy instead of Beanshell, way better for JMeter performances and way better for coding as Groovy is much more powerful for JSON and XML and maintained
The first reference link (alexandru-ersenie.com) has expired.
0

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

But it can be done with JSON Extractor since JMeter 3.0, this plugin has been donated by UbikLoadPack (http://jmeter.apache.org/changes_history.html)

Example:

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": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}
   ],
    "bicycle": {"color": "red","price": 19.95}} }

To have this count:

1/ Add JSON Extractor:

JSON Extractor configuration

The count will be then available bookTitle_matchNr which you can access through:

${bookTitle_matchNr}

Running this Test Plan would display this:

Run result

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

3 Comments

Thanks for the suggestion! Your plugin seems really useful, but for now I'll go with the Regular Expression Extractor solution that Dmitri mentioned. Thanks anyway!
No problem :-) , as Dmitri says, using Regexp for JSON is not usually a good thing, and Beanshell leads to lot of custom code and maintainability + productivity issues, in the end buying our plugin is better :-) , but if you go for coding , prefer the JSR223 + Groovy instead of Beanshell, Way better for JMeter performances and Way better for coding as Groovy is much more powerful and maintained
This answer should NOT be downvoted as it worked perfectly for me. And the JSON Extractor is no longer a plugin, but is now part of the release.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.