当键未知时如何从jmeter中的json中提取值? [英] How to extract values from json in jmeter when the keys are unkown?

查看:17
本文介绍了当键未知时如何从jmeter中的json中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

假设您有以下格式的响应:

<代码>{数据": {资产":{"sadsad12dwqqwe": "asda1212312","asdasd1213123": "asdas2131231"}}}

您可以使用

参考文献:

I have a json response as, { 'sadasd123242' : 'asdadada122dfsfs', 'dadsadaskljk' : 'adasdasdasdsadds' } I want to extract the keys from the response in jmeter test using the JSON extractor. I am unable to do this as I do not know the keys in the response. How do I get the keys ?

解决方案

Assuming you have the response in the following format:

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

You can extract key names using JSR223 PostProcessor and the following code:

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:

References:

这篇关于当键未知时如何从jmeter中的json中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆