Forum Discussion

sayee's avatar
sayee
New Contributor
9 years ago

How to assert count of a certain field in a JSON response is lesser than or equal to a certain value

Below is my sample response

 

I have right clicked on matchingaddress field in outline mode and selected add assertion-->for count and it gives me the json path expression as $.matchingAddresses.addressList in Expected result section i need to give the condition as <50 but it is not working as i assume it is exactly matching.

 

Can you please guide me how to implement this logic?

 

matchingAddresses": {
"addressList": [
{
"addressTxt": " 1050 10 AVE SW SALMON ARM BC V1E4M2 ",
"locationId": "2874394"
},
{
"addressTxt": "1 1050 10 AVE SW SALMON ARM BC V1E1T2 ",
"locationId": "2874389"
},
{
"addressTxt": "3 1050 10 AVE SW SALMON ARM BC NPC ",
"locationId": "2874390"
},
{
"addressTxt": "4 1050 10 AVE SW SALMON ARM BC NPC ",
"locationId": "2874391"
},
{
"addressTxt": "5 1050 10 AVE SW SALMON ARM BC V1E1T2 ",
"locationId": "2874392"
},
{
"addressTxt": "7 1050 10 AVE SW SALMON ARM BC V1E1T2 ",
"locationId": "2874393"
}

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    I am not well in jsonPath. But following script assertion (for the rest request step) can help you to achieve what you are looking for.

     

    Define a test case custom property, MAX_EXPECTED_SIZE and provide value for the same and now try your case.

     

     

    import net.sf.json.groovy.JsonSlurper
    def jsonSlurper = new JsonSlurper()
    def object = jsonSlurper.parseText(context.response)
    def expectedSize = context.testCase.getPropertyValue('MAX_EXPECTED_SIZE') as int
    assert object.matchingAddresses.addressList.size() < expectedSize, "Address List size is less than expected size"

     

  • bzzup's avatar
    bzzup
    Occasional Contributor

    Try XPath Match assertion with Xpath Expression like 

     

    boolean(count(//Response[1]/matchingAddresses[1]/addressList[1]/e) < 50)

    and Expected Result = True

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Sayee,

     

    I’m not familiar with JSONPath very well. Does it have count() (link in XPath) or length to get the number of the returned responses?