当密钥未知时,如何从jmeter中的json中提取值?


问题内容

我有一个JSON响应,例如{‘sadasd123242’:’asdadada122dfsfs’,’dadsadaskljk’:’adasdasdasdsadds’}我想
使用JSON提取器从jmeter测试中的响应中提取密钥。我无法执行此操作,因为我不知道响应中的键。我如何获得钥匙?


问题答案:

假定您具有以下格式的响应:

{
  "data": {
    "assets": {
      "sadsad12dwqqwe": "asda1212312",
      "asdasd1213123": "asdas2131231"
    }
  }
}

您可以使用JSR223PostProcessor
和以下代码提取键名称:

new groovy.json.JsonSlurper().parse(prev.getResponseData()).data.assets.eachWithIndex{ def node, int idx ->
    log.info('Key ' + idx + '=' + node.getKey())
    vars.put('key_' + idx, node.getKey())
}

It will print key names into jmeter.log file and create JMeter Variables
like:

- `${key_1}`
- `${key_2}`
- etc.

holding the required “key” values.

Demo:

JMeter Groovy Extract Keys From
JSON

References: