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