09-21-2022 12:43 PM
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":
}]
}
Solved! Go to Solution.
09-21-2022 01:44 PM
09-21-2022 01:06 PM
09-21-2022 01:31 PM
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?
09-21-2022 01:40 PM
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.
09-21-2022 01:44 PM
09-21-2022 02:17 PM
ah, simpler. Thank you.
01-15-2025 08:07 AM
"I have the following response:
[ { "customerId": "3756756", "phoneVerify": true }, { "customerId": "2661521", "phoneVerify": true } ]
Please help me. How can I count how many "phoneVerify": true there are?"
01-15-2025 09:43 AM - edited 01-15-2025 07:06 PM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide