jsonjmeterjmeter-5.0post-processor

How to extract multiple (random) values from the same json node


I'm trying to capture multiple values from a JSON Response I get but seem to be unable to get them from the same random node.

I've tried to place multiple variables in the same extractor using ";" and this works but it goes through the nodes randomly and doesn't extract the values I need from the same one

enter image description here

The source would be something like

[
	{
		"Disabled": false,
		"Group": null,
		"Selected": false,
		"Text": "Text1",
		"Value": "Value1"
	},
	{
		"Disabled": false,
		"Group": null,
		"Selected": false,
		"Text": "Text2",
		"Value": "Value2"
	}
]

and I would like to get from any of the 2 nodes (randomly matched) both the Text and Value in either a array that I can use or 2 variables.

So far it seems to take value from one node and text from another (in lengthier sources)

so my desired outcome would be either text1 and value1 or text2 and value2, not a mix of both..


Solution

    1. Add JSR223 PostProcessor as a child of the request which returns the above JSON
    2. Put the following code into "Script" area:

      def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
      def randomEntry = json.get(org.apache.commons.lang3.RandomUtils.nextInt(0, json.size()))
      vars.put('strBrandID', randomEntry.Value)
      vars.put('strBrandName', randomEntry.Text)
      
    3. That's it, you should be able to refer the Text/Value pairs as ${strBrandID} and ${strBrandName} where required

    More information: