2

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..

Dirk R.
  • 171
  • 1
  • 13

1 Answers1

3
  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:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Works like a charm, was hoping to use the built in extractor but seems that it's functionality is limited. An followup question tho, any easy way to add a default value or should I write an if clause arround it to check if randomEntry.Value has anything in it? – Dirk R. Dec 20 '18 at 13:49
  • 1
    If clause should work fine. Alternatively you can define these 2 variables with reasonable default values via i.e. [User Defined Variables](https://guide.blazemeter.com/hc/en-us/articles/207421395-Using-User-Defined-Variables) configuration element, if JSR223 PostProcessor fails to extract the data from the response - the variables values will not be overwritten and remain defaults. – Dmitri T Dec 20 '18 at 14:55
  • So is there no way to do this with the JSON extractor? – Max Coplan Mar 25 '22 at 23:29