Count Values in a JSON Array Returned From a REST API Call

My JSON Array returns the data. I want to get the total number of elements from API response.

Kindly help.

can you post a sample response json? eventualy edit the values if has sensitive data, we are interrested in the overal structure and key names.
and pleaaase, use code formating

in theory is easy. parse the json and check the size of the resulting list. in practice … may be different

Here is my response.
[
{
“name” : “Corp”,
“count” : 7,
“children” : [
{
“name” : “Gat”,
“count” : 7,
“id” : 32
} ],
“id” : 65,
“parentType” : true
},
{
“name” : “Sprocket”,
“count” : 1,
“children” : [
{
“name” : “Marttinn”,
“count” : 1,
“id” : 38
} ],
“id” : 55,
“parentType” : true
},
{
“name” : “Pack”,
“count” : 1,
“children” : [
{
“name” : “Pack”,
“count” : 1,
“id” : 38
} ],
“id” : 19,
“parentType” : true
},
{
“name” : “Technologies”,
“count” : 1,
“children” : [
{
“name” : “Total”,
“count” : 1,
“id” : 56
} ],
“id” : 80,
“parentType” : true
} ]

Try this Sudheer_D_J

import groovy.json.JsonSlurper as JsonSlurper

response = WS.sendRequest(findTestObject('Employees/Get Employees sang', [('Token') : GlobalVariable.Token, ('Data') : 'Token']))

def jsonSlurper = new JsonSlurper()
def parsedJson_count = jsonSlurper.parseText(response.getResponseText())
def expectedSize = parsedJson_count.size()

println("Total: " + expectedSize)

Thanks a lot Sang, it worked.

Hi @sang , i have one request where i need to fetch the size. Below is the JSON Response

I am expecting size as ‘1’. But actual i am getting size as ‘3’. Please suggest me. Only 1st level count i want the value

{
“id” : 84,
“hierarchyName” : “564”,
“ctego” : [
{
“id” : 123,
“ctId” : 5,
“ctName” : “Power”,
“ctCode” : “350”,
“display” : 0,
“itCount” : 100,
“img” : “Power.jpg”,
“children” : [
{
“id” : 456,
“pNodeId” : 123,
“ctId” : 38,
“ctName” : “Pulleys”,
“ctCode” : “3531”,
“display” : 0,
“itCount” : 10,
“img” : “Pulleys.jpg”,
“children” : [
{
“id” : 789,
“pNodeId” : 456,
“ctId” : 34,
“ctName” : “Sheaves”,
“ctCode” : “12131”,
“display” : 0,
“itCount” : 10,
“leafNode” : true
} ],
“leafNode” : false
} ],
“leafNode” : false
} ]
}

the result is corect.
jsonParse will build a lazy map from the text json.
since you have this time an object with three keys (id, hierarchy, and ctego) in the root object, you will get a collection with three key:value elements

Thanks for the response @Ibus .

How to get the size separately?

what size you need, to be specific?
you have to distinguish between object and aray response.
based on your needs on specific data, we can design a custom keyword. but you have to help us with more info about your api repsonses and expected results

Hi @Ibus,

{
“id” : 84,
“hierarchyName” : “564”,
“ctego” : [
{
“id” : 123,
“ctId” : 5,
“ctName” : “Power”,
“ctCode” : “350”,
“display” : 0,
“itCount” : 100,
“img” : “Power.jpg”,
“children” : [
{
“id” : 456,
“pNodeId” : 123,
“ctId” : 38,
“ctName” : “Pulleys”,
“ctCode” : “3531”,
“display” : 0,
“itCount” : 10,
“img” : “Pulleys.jpg”,
“children” : [
{
“id” : 789,
“pNodeId” : 456,
“ctId” : 34,
“ctName” : “Sheaves”,
“ctCode” : “12131”,
“display” : 0,
“itCount” : 10,
“leafNode” : true
} ],
“leafNode” : false
} ],
“leafNode” : false
} ]
}

Bolded text is Children. So I don’t want those children count. I want only the parent count(1st level i.e ‘1’)

My UI looks something like
1
1.1
2.1

this you have in the root object. three keys.
the rest does not matter. an array is not an object as an object is not array. in java, a map is not a list and a list is not a map. they iterate completely different

to reach the ‘children’ you have to pick first element of ctego field. or second. or third …

Hi Sudheer_D_J, I have the same thought as Ibus. It might help if you explain in other ways on the data that you need to count.

Thank you @sang and @Ibus, your solution worked for me.