如何根据条件从 JSON 路径提取器中提取值

How to extract value from JSON Path extractor based on condition

我想根据条件“status”获取“currentApproversStr:”的值:“Ready for Review” JSON HTTP 采样器的响应正文并传递给以下 HTTP采样器。

我尝试了下面的方法,但它不起作用

  1. 创建变量的名称:currentApproversStr
  2. JSON 路径表达式:$.[?((@.currentApproversStr == "Validation, Civa" || @.currentApproversStr == "Validation, Darla" || @.currentApproversStr == "Validation , Bittl" || @.currentApproversStr == "Validation, Cha" || @.currentApproversStr == "Validation, Barnett" ) && @.status== "Ready for Review")]
  3. 匹配编号:-1 或 1

但是虚拟采样器returns结果

我们无法保证“timecardId”块的顺序为“status”:“Ready for Review”,即有时排在第二位,有时排在最后。这是第二个街区。所以不确定匹配号:我应该给什么

[
  {
    "timecardId": 170803,
    "entryHeaderId": "db9341a9-32e8-4d45-a858-a88b75a42cef",
    "startsOn": "2021-10-24T00:00:00",
    "endsOn": "2021-10-30T00:00:00",
    "worksightStatus": "SignedOff",
    "projectId": 1977,
    "userId": 60874,
    "status": "Submitted for Approval",
    "batchId": 39814,
    "emergencyType": "",
    "htgDealMemoId": "0d0ff42b-5c4b-4695-b527-34dfc64585e5",
    "unionId": "1c77c660-28fc-4e40-b557-132f3da39597",
    "currentApproversStr": "Perf, PA",
    "commentStr": "",
    "commentUserName": "",
    "commentCreatedAt": "1900-01-01T00:00:00",
    "occupationCode": "TECHNICIAN",
    "activeApprovalFlowId": 166669,
    "isAllowanceOnly": false,
    "departmentId": null,
    "datePosted": null
  },
  {
    "timecardId": 170807,
    "entryHeaderId": "c9809446-b01f-4f42-add6-9b441c3d0114",
    "startsOn": "2021-10-17T00:00:00",
    "endsOn": "2021-10-23T00:00:00",
    "worksightStatus": "Outstanding",
    "projectId": 1977,
    "userId": 60874,
    "status": "Ready for Review",
    "batchId": 39815,
    "emergencyType": "",
    "htgDealMemoId": "0d0ff42b-5c4b-4695-b527-34dfc64585e5",
    "unionId": "1c77c660-28fc-4e40-b557-132f3da39597",
    "currentApproversStr": "Validation, Civa",
    "commentStr": "",
    "commentUserName": "",
    "commentCreatedAt": "1900-01-01T00:00:00",
    "occupationCode": "TECHNICIAN",
    "activeApprovalFlowId": 166674,
    "isAllowanceOnly": false,
    "departmentId": null,
    "datePosted": null
  },
  {
    "timecardId": 170802,
    "entryHeaderId": "db9341a9-32e8-4d45-a858-a88b75a42cef",
    "startsOn": "2021-10-24T00:00:00",
    "endsOn": "2021-10-30T00:00:00",
    "worksightStatus": "SignedOff",
    "projectId": 1977,
    "userId": 60874,
    "status": "Submitted for Approval",
    "batchId": 39814,
    "emergencyType": "",
    "htgDealMemoId": "0d0ff42b-5c4b-4695-b527-34dfc64585e5",
    "unionId": "1c77c660-28fc-4e40-b557-132f3da39597",
    "currentApproversStr": "Perf, PA",
    "commentStr": "",
    "commentUserName": "",
    "commentCreatedAt": "1900-01-01T00:00:00",
    "occupationCode": "TECHNICIAN",
    "activeApprovalFlowId": 166669,
    "isAllowanceOnly": false,
    "departmentId": null,
    "datePosted": null
  }
]

“匹配号”的工作原理如下:如果您的查询 returns 超过 1 个结果:

  1. 0 - returns 随机结果

  2. -1 - returns 所有结果的形式为:

    • currentApproversStr_1 - 第一场比赛
    • currentApproversStr_2 - 第二场比赛
    • 等等
    • currentApproversStr_matchNr - 匹配总数
  3. any positive integer - returns 给定的匹配项

它不仅适用于从响应中提取值的JSON Extractor but to all other JMeter PostProcessors

可以看到生成了JMeter Variables using Debug Sampler and View Results Tree listener combination:

问题: 原因是您误解了 JSON 提取器的工作方式。此功能允许您在一个设置中提取多个变量,但是 Names of created variables 的数量 = JSON Path expressions 的数量 = Default Values 的数量。

比如你要提取2个变量:

  • 创建的变量名称:var_name_1var_name_2

  • JSON路径表达式:json_expression_1json_expression_2

  • 默认值:default_1default_2

(注意:记得用分号(;)分隔值)

但是你设置了1变量,1json表达式用MANY默认值 --> 不匹配。

解决方案: 您可以这样设置:

Names of created variables: currentApproversStr
JSON Path expressions: $.[?(@.status== "Ready for Review")].currentApproversStr
Match No: -1
Default Values: NOT_FOUND

结果:

currentApproversStr_1=Validation, Civa
currentApproversStr_matchNr=1