How to get the count of value-pair in an array from the response body?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 09:06 AM
Hi All,
I would like to get the count of the key value from the response body.
Response Body :
[
{
"task":"7928f59b1bdaf5104e90edb1604XXXXXX",
"entities":"["base","base"]",
"name":"Create an Incident",
"title":"Create an Incident",
"type":"normal",
"utterance":"base",
"prediction":"Create an Incident",
"modelId":"ml_x_itfcu_global_global_916756f81b6de550711eXXXXXXXXXX",
"sys_id":"041842d21bf7e9104e90edb1605bcbfa"
},
{
"task":"7928f59b1bdaf5104e90edb1604XXXXXX",
"entities":"["base","base"]",
"name":"Submit a Request",
"title":"Submit a Request",
"type":"normal",
"utterance":"base",
"prediction":"SubmitARequest",
"modelId":"ml_x_itfcu_global_global_916756f81b6de550711e97XXXXXXXXXX",
"sys_id":"0e4916191b166d10515a1f09b04bcb38"
}
]
How do I get both the sys id values and the count?
Sujatha V.M.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 09:13 AM
You can count number of objects = responseBody.length; (Please make sure that your responseBody must be parsed before using it, if it is a string it doesn't work, for parsing the responsebody, you can do JSON.parse(responseBody))
for parsing the json you need to do as follows:
var sysID = [];
for(var i = 0 ; i<responseBody.length ; i++){
var obj = responseBody[i];
sysID.push(obj.sys_id) // this will give you sys_id
}
//you cam use the sysID array for your scenario
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 09:44 AM
@Sujatha V M Here is the script you should use.
var someString = '[ { "task":"7928f59b1bdaf5104e90edb1604XXXXXX", "entities":["base","base"], "name":"Create an Incident", "title":"Create an Incident", "type":"normal", "utterance":"base", "prediction":"Create an Incident", "modelId":"ml_x_itfcu_global_global_916756f81b6de550711eXXXXXXXXXX", "sys_id":"041842d21bf7e9104e90edb1605bcbfa" }, { "task":"7928f59b1bdaf5104e90edb1604XXXXXX", "entities":["base","base"], "name":"Submit a Request", "title":"Submit a Request", "type":"normal", "utterance":"base", "prediction":"SubmitARequest", "modelId":"ml_x_itfcu_global_global_916756f81b6de550711e97XXXXXXXXXX", "sys_id":"0e4916191b166d10515a1f09b04bcb38" }]';
var someObj = JSON.parse(someString);
gs.info('Count of keys '+Object.keys(someObj[0]).length);
gs.info('sys_id of 1st element '+someObj[0].sys_id);
gs.info('sys_id of 2nd element '+someObj[1].sys_id);