简体   繁体   English

JMeter:如何使用jsonpath计算数组中的JSON对象

[英]JMeter: How to count JSON objects in an Array using jsonpath

In JMeter I want to check the number of objects in a JSON array, which I receive from the server. 在JMeter中,我想检查从服务器收到的JSON数组中的对象数。

For example, on a certain request I expect an array with 5 objects. 例如,在某个请求中,我期望一个包含5个对象的数组。

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

After reading this: count members with jsonpath? 看完之后: 用jsonpath计算成员? , I tried using the following JSON Path Assertion: ,我尝试使用以下JSON路径断言:

  • JSON Path: $ JSON路径:$
  • Expected value: hasSize(5) 预期价值: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. 当我确实在数组中接收到5个对象时,响应断言表明它不匹配。

What am I doing wrong? 我究竟做错了什么? Or how else can I do this? 或者我怎么能这样做?

Although JSONPath Extractor doesn't provide hasSize function it still can be done. 虽然JSONPath Extractor不提供hasSize函数,但仍然可以完成。

Given the example JSON from the answer by PMD UBIK-INGENIERIE, you can get matches number on book array in at least 2 ways: 鉴于PMD UBIK-INGENIERIE答案中的示例JSON,您可以通过至少两种方式获得book数组的匹配数:

1. Easiest (but fragile) way - using Regular Expression Extractor . 1.最简单(但最脆弱)的方式 - 使用正则表达式提取器

As you can see, there are 4 entries for category like: 如您所见,有4个category条目,如:

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

If you add a Regular Expression Extractor configured as follows: 如果添加如下配置的正则表达式提取器:

JSON正则表达式

It'll capture all the category entries and return matches number as below: 它将捕获所有category条目并返回匹配号码如下:

JSON正则表达式匹配

So you will be able to use this ${matches_matchNr} variable wherever required. 因此,您可以在需要的地方使用此${matches_matchNr}变量。

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. 如果您预计JSON数据可能在可预见的未来发生变化,请继续使用下一个选项。

2. Harder (but more stable) way - calling JsonPath methods from Beanshell PostProcessor 2.更难(但更稳定)的方式 - 从Beanshell PostProcessor调用JsonPath方法

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. JMeter有一个Beanshell脚本扩展机制,可以访问范围内的所有变量/属性以及底层JMeter和第三方依赖项API。 In this case you can call JsonPath library (which is under the hood of JsonPath Extractor) directly from Beanshell PostProcessor. 在这种情况下,您可以直接从Beanshell PostProcessor调用JsonPath库(位于JsonPath Extractor的引擎盖下)。

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 上面的代码针对父采样器响应计算$.store.book[?] JSONPath表达式,计算匹配数并将其存储到${JSON_ARRAY_SIZE} JMeter变量中

JSON Beanshell变量

which can later be reused in an if clause or an assertion. 以后可以在if子句或断言中重用。

References: 参考文献:

This is not possible with the plugin you are using (JMeter-plugins). 使用您正在使用的插件(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 ) 但是自JMeter 3.0以来可以用JSON Extractor完成,这个插件已经由UbikLoadPack捐赠( http://jmeter.apache.org/changes_history.html

Example: 例:

Say you have this JSON that contains an array of books: 假设你有这个包含一系列书籍的JSON:

   { "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: 1 /添加JSON提取器:

JSON Extractor配置

The count will be then available bookTitle_matchNr which you can access through: 然后,计数将可用bookTitle_matchNr,您可以通过以下方式访问:

${bookTitle_matchNr} $ {} bookTitle_matchNr

Running this Test Plan would display this: 运行此测试计划将显示以下内容:

运行结果

As you can see, Debug Sampler-${bookTitle_matchNr} shows Debug Sampler-4 如您所见,Debug Sampler - $ {bookTitle_matchNr}显示Debug Sampler-4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM