Search code examples
jmeterjmeter-plugins

JMeter Assertion on Count of Json elements greater than X elements


I need to assert count of Json elements > x number. I know we have a Json extractor to get the count. But just thinking if there is any other efficient way to directly assert the Json count from the response. For example. From below Json response. I need to assert if my response contains minimum 3 Json elements. Any help on this

[{"ID":y1,"Name":"Sunil"},
{"ID":x1,"Name":"Suresh"},
{"ID":x3,"Name":"hari"},
{"ID":s1,"Name":"Arun"}]

Solution

  • You can parse JSON and compare it with the acceptance criteria in a single JSR223 Assertion, example code would be something like:

    def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    
    def responseSize = response.size()
    
    if (responseSize < 3) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('Number of entries in the response is ' + responseSize)
    }
    

    More information: