【问题标题】:jmeter: I want to extract 2 values from 2 different json objects where 1 json object will validate my condition & other value I want to extractjmeter:我想从 2 个不同的 json 对象中提取 2 个值,其中 1 个 json 对象将验证我的条件和我想提取的其他值
【发布时间】:2020-03-11 07:47:10
【问题描述】:

我的 JSON 响应是:

{
"results": [
    {
      "attributes": [
        {
          "format": "internal",
          "name": "resourceid",
          "type": "STRING",
          "value": "56B15190000015E85E57923F0000033B"
        },        
        {
          "format": "attribute",
          "name": "ds6w:identifier",
          "type": "string",
          "value": "ald7_al"
        }
      ]
    },
    {
      "attributes": [
        {
          "format": "internal",
          "name": "resourceid",
          "type": "STRING",
          "value": "56B15190000015E85E578B1F000001B6"
        },        
        {
          "format": "attribute",
          "name": "ds6w:identifier",
          "type": "string",
          "value": "fbh1"
        }
      ]
    },
    {
      "attributes": [
        {
          "format": "internal",
          "name": "resourceid",
          "type": "STRING",
          "value": "56B15190000015E85E578F7800000211"
        },
        {
          "format": "attribute",
          "name": "ds6w:identifier",
          "type": "string",
          "value": "u89cf"
        }
      ]
    }    
  ]
}

我想得到 '56B15190000015E85E57923F0000033B' 其中 value='ald7_al'

所以基本上在一个 jsonarray 中我有 jsonobjects,对于单个 jsonobject 我有两个 jsonobjects,其中 secong jsonobject 将验证我的条件参数,我想要来自第一个 jsonobject 的值

为了获得结果以解决我使用过的条件检查
JSON 提取器表达式为 -> $..attributes[?(@.value==ald7_al)] 这给了我第二个 json 块,但我想要来自第一个 json 块的值。

如果您有任何意见,请帮助我。 提前感谢您的帮助!

【问题讨论】:

  • 好像有no way可以访问找到的节点的父节点。

标签: json regex jmeter extractor


【解决方案1】:

JMeter 5.2.1 开始,使用内置 JMeter 组件是不可能的

所以你只剩下JSR223 PostProcessorGroovy 语言,应该解决你的问题的示例代码如下:

def results = new groovy.json.JsonSlurper().parse(prev.getResponseData()).results

0.upto(results.size() - 1, { index ->
    def attributes = results[index].attributes
    if (attributes[1].get('value').equals('ald7_al')) {
        vars.put('value', attributes[0].get('value'))
    }
})

将其添加为返回上述 JSON 的请求的子项,如果一切顺利,您将能够在需要的地方以 ${value} 的形式访问您要查找的值。

更多信息:

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    相关资源
    最近更新 更多