cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1106
Views
11
Helpful
7
Replies

Get Array Count from Web Svc Call in Call Studio

SANJ21
Level 1
Level 1

Running CVP v12.6. I am using the Rest_Client element in CS to do a web service call and am getting a JSON array response back. I have a requirement to handle calls based on the array counts. Within an Action Element-Set Value element, I tried doing a JSON.parse and using the length() to get the count but am either getting an error or null value depending on the iterations I tried. Can someone provide the right syntax if this can be done? The JSON data I get back is in this format below:

{"Results":[
   {"Name":
    "Address":
    "Phone":
   }
   {"Name":
    "Address":
    "Phone":
   }]
}

1 Accepted Solution

Accepted Solutions

No. You ONLY need to do this one line - it'll return the length into
your localVariable:

JSON.parse({Data.Element.restZipcodeLookup.response_body}).Results.length;



View solution in original post

7 Replies 7

janinegraves
Spotlight
Spotlight
Use this: JSON.parse({Data.Element.Rest01.response_body}).Results.length;



Thanks Janine. I have the following in an Action Element - Set Value:

var val = {Data.Element.restZipcodeLookup.response_body};

var path = JSON.parse({Data.Element.restZipcodeLookup.response_body}).Results.length;

JSONPathUtil.eval(val,path);

 

However, looking at the logs, I am seeing the value as null. I also have a Decision element after this to do other things if the count is more than the current counter but it's failing there as well...probably because I'm not getting a valid value. Any ideas?

NM.

This looks like how it should be:

var val = JSON.parse({Data.Element.restZipcodeLookup.response_body}).Results.length;

var path = "$";

JSONPathUtil.eval(val,path);

 

Just need to fine tune my app to handle the result.

No. You ONLY need to do this one line - it'll return the length into
your localVariable:

JSON.parse({Data.Element.restZipcodeLookup.response_body}).Results.length;



ah, simpler. Thank you.

"I have the following response:

[
  {
    "customerId": "3756756",
    "phoneVerify": true
  },
  {
    "customerId": "2661521",
    "phoneVerify": true
  }
]
Copy

Please help me. How can I count how many "phoneVerify": true there are?"

 

Try this in an action element:

importPackage(com.audium.server.cvpUtil);
var json = {Data.Element.GetJSON.response_body};
var obj = JSON.parse(json);
var phoneVerifyCount = obj.filter(item => item.phoneVerify === true).length;
phoneVerifyCount;

Disregard what I said above, I was going by memory and added regular JS to CVP. Here's how you get the count:

var json = '[ { "customerId": "3756756", "phoneVerify": true }, { "customerId": "2661521", "phoneVerify": true}]'
var obj = JSON.parse(json)
obj.length

Now you'll need to loop to the results if what you want is to only count where phoneVerify is true.

david