Bastian
Bastian

Reputation: 1237

Validate Json response in groovy + Jmeter

I have a question about a script that I would like to write. I send request and the response is json. to day I take the response to online tool like https://jsonlint.com/ that checks if it is a valid json. Is there a way to get this in jmeter just boolean valid or Not valid. regards

the code I wrote:

def json = new groovy.json.JsonSlurper.parseText(prev.getResponseDataAsString());

the error I get

unable to resolve class groovy.json.JsonSlurper.parseText 

Upvotes: 0

Views: 2018

Answers (2)

Dmitri T
Dmitri T

Reputation: 168072

You need parentheses after defining a new JsonSlurper instance

def json = null
try {
    json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
                                    //^^ here is your problem
    log.info("Valid JSON")
}
catch (Exception ex) {
    log.info("Invalid JSON")        
}

boolean valid = json != null;
// do what you need with this "valid" boolean

References:

Upvotes: 1

daggett
daggett

Reputation: 28564

try to parse json and if there is an error you will get parse exception.

def json = new groovy.json.JsonSlurper().parseText( jsonText )

Upvotes: 0

Related Questions