SOAPUI: How to find node count in JSON response

三世轮回 提交于 2019-12-08 03:02:13

问题


I have JSON response below in SOAP UI:- I need to find number of JSON Nodes in this JSON. I tried with JSONPATH Count but it doesn't give accurate result. Here 8 nodes are there wich is expected as output.

<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>

How to find Node Counts?


回答1:


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"



来源:https://stackoverflow.com/questions/35623183/soapui-how-to-find-node-count-in-json-response

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