I need JSON Extractor to only work when the condition is satisfied

27 Views Asked by At

In JMeter I need JSON Extractor to only extract, if a condition is satisfied, since if it tries to extract data that does not exist, it will not let the sampler that its attached to go. In the screenshot you can see ticketId <<< JSON, i need it to not start extracting if totalRecords = 0

I hope there is a tool suitable for that in JMeter

1

There are 1 best solutions below

0
Ivan G On

Unfortunately as of JMeter 5.6.3 you cannot apply PreProcessors conditionally. There is Default Values section where you can provide some placeholder value in case if extraction fails:

enter image description here

It will be stored into a JMeter Variable and then the variable can be used later on for example in If Controller


Alternatively you can use JSR223 PostProcessor instead of JSON Extractor,

Groovy has built-in JSON support and it's also possible to use the same JsonPath library which is used in the JSON Extractor:

Something like:

if (vars.get('recordsTotal') != '0') {
    vars.putObject('your-variable-name-here', com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), 'your-jsonpath-expression-here'))
}

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