4

I'm currently testing a web application which outputs json. I want to make sure that the JSON is valid. I do not want to validate its content. How can I implement a assertion in jmeter to make sure that the json response is valid?

Thanks!

bkone
  • 251
  • 1
  • 5
  • 15

2 Answers2

4

Use beanshell assertion, jsr223 assertion or bsf assertion if you want to use groovy:

and parse the response using some json parser:

Jmeter will provide to your script the previous sampler response as a script variable.

Note that this can impact performance of your test plan as it's rather costly.

Regards

Community
  • 1
  • 1
4

I normally use javascript BSF Assertion. Since JSON are javascript objects you can try to eval the response in a try-catch block and in case it fails you can explicitly fail the previous SampleResult.

try {
   eval('var response = ' + prev.getResponseDataAsString());
} catch(e) {
      prev.setSuccessful(false);
      prev.setResponseMessage("Invalid response. Expected a valid JSON.");
}
Fábio Uechi
  • 807
  • 7
  • 25