SOAPUI: How to find node count in JSON response

☆樱花仙子☆ 提交于 2019-12-06 14:29:23

You may extract the list and use size() method on it get the count using groovy script.

For example:

def employees = '''
[
    {
        "EmployeeId": "88",
        "EmployeeName": "John",
        "Role": "Dr"
    },
    {
        "EmployeeId": "999",
        "EmployeeName": "Doe",
        "Role": "Dr"
    }
]'''

def employeeList = new groovy.json.JsonSlurper().parseText( employees )
println employeeList.size()

Output is 2 which is expected.

Update:

If you want to the specific data that you provided, here you go.

Since the data is not directly json type, i.e., wrapped in cdata which is part of xml.
So first need to extract json data from xml then apply size to get the count.

def xml = '''
<data contentType="text/plain" contentLength="772"><![CDATA[[{
    "EmployeeId": "99",
    "EmployeeName": "Doe",
    "Role": "Dr"
},
{
    "EmployeeId": "88",
    "EmployeeName": "John",
    "Role": "Dr"
},
{
    "EmployeeId": "999",
    "EmployeeName": "Doe",
    "Role": "Dr"
},
{
    "EmployeeId": "888",
    "EmployeeName": "John",
    "Role": "Dr"
},
{
    "EmployeeId": "777",
    "EmployeeName": "Keerthi",
    "Role": "Dr"
},
{
    "EmployeeId": "666",
    "EmployeeName": "Keerthi",
    "Role": "Dr"
},
{
    "EmployeeId": "1234",
    "EmployeeName": "Sushant",
    "Role": "Doctor"
},
{
    "EmployeeId": "107",
    "EmployeeName": "John8",
    "Role": "LabTech"
}]]]></data>'''
def data = new XmlSlurper().parseText(xml)
def list = new groovy.json.JsonSlurper().parseText( data.toString())
log.info list.size()

You may also assert it using :
assert 8 == list.size(), "Employee count is not matching"

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!