Siddish
Siddish

Reputation: 97

Obtaining the child values randomly in JSON

I have a JSON like below: I need to extract the Options -> Child as a Random and also Values within the options as randomly. How can we achieve in jmeter ?

{
"id":37,
"merchant_id":"39",
"title":"Parker Pens",
"subtitle":null,
"price":1000,
"description":null,
"images":[  ],
"image_thumbs":[  ],
"options":[  
 {  
     "code":"color",
     "label":"Color",
     "extra_info":"",
     "values":[  
        {  },
        {  },
        {  }
     ]
  },
  {  
     "code":"size",
     "label":"Size",
     "extra_info":"",
     "values":[  
        {  },
        {  },
        {  }
     ]
  }
],"options_available":[  
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  }
], "custom_options":[  

 ]
 }

I have to fetch the child of options randomly . In that i have to fetch the value of "Code" and its associated value within the "Value" . Help is appreciated and useful

Upvotes: 0

Views: 485

Answers (1)

Dmitri T
Dmitri T

Reputation: 168132

Your requirements are a little bit vague as you haven't indicated what is the desired output format. One of the solutions would be using JSR223 PostProcessor in order to obtain the random value from random options array like:

import com.jayway.jsonpath.JsonPath
import org.apache.commons.lang3.RandomUtils
import org.apache.jmeter.samplers.SampleResult

def options = JsonPath.read(prev.getResponseDataAsString(), '$.options')
def randomOption = options.get(RandomUtils.nextInt(0, options.size()))
def values = randomOption.get('values')
def randomValue = values.get(RandomUtils.nextInt(0, values.size())) as String
vars.put('randomValue', randomValue)

References:

Upvotes: 1

Related Questions