Forum Discussion

Gkm's avatar
Gkm
Contributor
8 years ago
Solved

Soapui Script Assertion, when Json response return as string

  My Json response(which return as string),   "[{\"sysSerial\":5,\"policyName\":\"hold\",\"uploadPolicyTypes\":[{\"sysSerial\":36,\"uploadPolicyId\":5,\"type\":null,\"isUploadMetaData\":true}]}]" ...
  • Gkm's avatar
    Gkm
    8 years ago

    So I have fixed my own problem by simple regular expressions , Thank you nmrao  once again for giving me an idea.

    Here is the code,

     

    //imports
    import groovy.json.JsonSlurper
    
    //grab the response
    def ResponseMessage = messageExchange.response.responseContent
    
    // replace "(starting&ending), [](starting&ending) and \ with space from response
    def TrimResponse =ResponseMessage.replaceAll('^\"|\"$','').replaceAll('^\\[|\\]$','')replaceAll('\\\\','')
    
    //define a JsonSlurper
    def jsonSlurper = new JsonSlurper().parseText(TrimResponse)
    
    //verify the slurper isn't empty
    assert !(jsonSlurper.isEmpty())
    
    assert jsonSlurper.sysSerial != null
    assert jsonSlurper.sysSerial == 5
    assert jsonSlurper.uploadPolicyTypes[0].sysSerial == 36

    Cheers,

    Geeta