How to extract values from json in jmeter when the keys are unkown?

后端 未结 2 921
后悔当初
后悔当初 2021-01-27 07:32

I have a json response as, { \'sadasd123242\' : \'asdadada122dfsfs\', \'dadsadaskljk\' : \'adasdasdasdsadds\' } I want to extract the keys from the response in jmeter test u

2条回答
  •  星月不相逢
    2021-01-27 08:24

    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:

    • Apache Groovy: Parsing and producing JSON
    • Apache Groovy - Why and How You Should Use It

提交回复
热议问题