Home > Net >  How to parse array values from Json in Jmeter
How to parse array values from Json in Jmeter

Time:11-24

For the below response I need to fetch the rideId and pass it to the next request in Jmeter.Also ,the API that generates below response should be executed until the eventType is HANDSHAKE.

 [{"id":90856,"eventType":"HANDSHAKE","parameters":"{\"handshakeExpiration\":1669217518986,\"rideId\":3107}"}]

I am using the code :

import groovy.json.JsonSlurper;
def jsonSlurper=new JsonSlurper();
def apiDetailsArr=jsonSlurper.parse(prev.getResponseData())
def apiDetails=apiDetailsArr.size()>0?apiDetailsArr.get(0):null
def shouldRun = "1"
if(apiDetails!=null)
{
log.info("details",apiDetails.eventType "")
if(apiDetails.eventType="HANDSHAKE"){
shouldRun="0"
}
def object=jsonSlurper.parseText(apiDetails.parameters)
log.info("xyz",object "")

def id=object.rideId;
log.info("id",id "")
vars.put("id", id "")
}
else{
shouldRun="1"`enter code here`
}  


 `Condition for while controller :     `${__javaScript( "${shouldRun}" != "0",)}``

CodePudding user response:

  1. All your log.info function calls cause syntax errors, you can see it yourself in (surprise!) the jmeter.log file
  2. I fail to see where you're assigning shouldRun JMeter Variable which is used in the While Controller

Suggested code change:

import groovy.json.JsonSlurper;

def jsonSlurper = new JsonSlurper();
def apiDetailsArr = jsonSlurper.parse(prev.getResponseData())
def apiDetails = apiDetailsArr.size() > 0 ? apiDetailsArr.get(0) : null
def shouldRun = "1"
if (apiDetails != null) {
    log.info("details", apiDetails.eventType   "")
    if (apiDetails.eventType = "HANDSHAKE") {
        shouldRun = "0"
    }
    def object = jsonSlurper.parseText(apiDetails.parameters)
    log.info("xyz"   object)

    def id = object.rideId;
    log.info("id"   id)
    vars.put("id", id as String)
} else {
    shouldRun = "1"
}  

vars.put("shouldRun", shouldRun)

More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

  • Related