Search code examples
jsonweb-api-testing

How to get specific value from Json response using SOUPUI


I am very new to SOUPUI, I am able to read the response for restapi and able to save to file, I struked at this place i want to store a value from the response. Please help.

My questions :

  1. How can I store a random employeeId from the following response.(Pick any of the employeeId randomly)?
  2. How to get specific employeeId using employee name(Like i want to get "employeeId" if "firstName" name is "Daitha")?
  3. How can i read the employeeId using array (Like first , second ..employeeId)?

Following is the sample response:

{
    "response": {
        "stat": "SUCCESS",
        "result": {
            "employees": [{
                "employeeId": "7d58129a-5ca3-4acd-a601-11478ba47988",
                "firstName": "Daitha",
                "lastName": "shankar",
                "loginName": "[email protected]",
                "mobileNumber": "xxxxxxxxxxxx",
                "emailId": "[email protected]",
                "rowStatus": "A",
                "assetCount": 3087,
                "canUseapp": "Y",
                "supportUserInd": "N"
            }, {
                "employeeId": "e2dec6de-8882-4c5b-a875-41fffe8e977f",
                "firstName": "john",
                "lastName": "deo",
                "loginName": "[email protected]",
                "emailId": "[email protected]",
                "rowStatus": "A",
                "assetCount": 0,
                "canUseapp": "Y",
                "supportUserInd": "N"
            }, {
                "employeeId": "9a9e7ff6-edb7-402a-bed2-27e9036a716f",
                "tenantBadgeId": "EMP11659824",
                "firstName": "suman",
                "lastName": "m",
                "loginName": "[email protected]",
                "mobileNumber": "xxxxxx",
                "emailId": "[email protected]",
                "rowStatus": "A",
                "designation": "software QA engineer",
                "assetCount": 0,
                "canUseapp": "Y",
                "supportUserInd": "N"
            }, {
                "employeeId": "9ecf7fc8-c06d-4e3c-a3a3-d2c50509c16b",
                "firstName": "vinay",
                "lastName": "B",
                "loginName": "[email protected]",
                "emailId": "[email protected]",
                "rowStatus": "A",
                "assetCount": 0,
                "canUseapp": "Y",
                "supportUserInd": "N"
            }]
        }
    }
}

Thanks in Advance


Solution

  • Shankar,

    you can play around with below groovy script

    import groovy.json.JsonSlurper
    def responseContent = testRunner.testCase.getTestStepByName("Emp").getPropertyValue("response")
    def r = new JsonSlurper().parseText(responseContent)
    def employeesSize = r.response.result.employees.size()
    //log.info "employee size:"+employeesSize
    def list=new Object[4]
    for(int i=0;i<employeesSize;i++)
    {
        //log.info r.response.result.employees[i].firstName
        list[i] = r.response.result.employees[i].lastName
        log.info list[i]
    }
    //log.info(list[3])
    //log.info "stat:"+r.response.stat
    for(int i=0;i<list.size();i++)
    {
        //list.each{log.info(it)}
        def p=list[i]
        if(p=="vinay")
        {
            //log.info "yes"
            log.info r.response.result.employees[i].employeeId
        }
        else
        {
            //log.info "no"
        }
    }
    //log.info r.response.result.employees[0].size()
    def listempdata = r.response.result.employees[0]
    //log.info listempdata
    def k =new Object[r.response.result.employees[0].size()]
    for(int j=1;j<r.response.result.employees[0].size();j++){
        //k[j]=listempdata.each{it}.toString().split(",")
        //listempdata.each{log.info (it)}
        //log.info listempdata.lastName 
    }