06-07-2021 10:42 AM
Hey everyone. I am calling an API and need to parse out the results but am having a difficult time doing so. I am working with CVP 11.6 and returning the following information:
{ "ResponseSample":"{\"StartDate\":\"2020-08-01\",\"EndDate\":\"2021-07-31\",\"Status\":\"Review\"}" }
I am trying to get the "StartDate" value, however CVP is returning either a 'null' or blank data. I tried using the JSON path of $.StartDate but that does not work and returns the 'null' text in my debugger. I also tried to parse within the app, however not sure if my syntax is correct as the variable is blank. The node is currently set as follows:
importPackage(com.audium.server.cvpUtil); var resp= JSON.parse({Data.Element.GetUserCoverage.response_body}); var startDate = resp.StartDate;
That value is then set in SessionData to validate. Any help on this would be greatly appreciated.
Solved! Go to Solution.
06-14-2021 04:52 AM
Finally! Here is the correct syntax for hard coding JSON strings:
importPackage(com.audium.server.cvpUtil); var json = '{"CoverageStartDate":"2021-08-15","CoverageEndDate":"2022-08-14","Status":"Submitted"}' var path = "$.CoverageStartDate"; JSONPathUtil.eval(json, path);
Hope this helps others!
06-07-2021 12:53 PM
Check out this blog I wrote a while back to see if it helps https://dmacias.org/cisco-courtesy-callback-ccb-and-cce-hours-of-operations/. Main part being how you're parsing the data:
dowPath=
"/businessHour/weekDaySchedules/weekDaySchedule
1/dayOfWeek"
dow = XpathUtil.eval(xml,dowPath)
06-08-2021 02:25 AM
David below example is parsing XML. Here is an example for parsing JSON.
JSON Data
[{"CallbackId":790225,"CustomerName":"Bob Bobster","Description":"Sales Chat","Created":"Jun 1 2021 1:41PM","Expiry":"Jun 8 2021 1:41PM"}]
TIP - use a JSON Path tester to get the format correct for your JSON!
SET VALUE Element sets the following 3 local variables (note the first one has to import the CVP Util package).
AgentId
importPackage(com.audium.server.cvpUtil); var val= {Data.Element.Rest_Client_GetCallback.response_body} var path = "[0].AgentId"; JSONPathUtil.eval( val, path);
CustomerName
var path = "[0].CustomerName"; JSONPathUtil.eval( val, path);
Description
var path = "[0].Description"; JSONPathUtil.eval( val, path);
06-08-2021 06:36 AM
06-09-2021 05:49 AM
Thank you both for the input (and awesome solution to the CCB timing @david.macias). I think my issue is how I am receiving the data from the vendor. The leading "ResponseSample" is the object and I can't navigate to the needed key/value pair. With removing that and surrounding double quotes I am able to filter each key:
{"CoverageStartDate":"2020-08-01","CoverageEndDate":"2021-07-31","Status":"Pending Review"}
My issue now is in Call Studio, when I hardcode that in a variable I am receiving an error stating that I cannot set the value as it's an array...even though it isn't.
Error:
Wrapped com.jayway.jsonpath.PathNotFoundException: Path 'CoverageStartDate' is being applied to an array. Arrays can not have attributes.
Variable Setting:
importPackage(com.audium.server.cvpUtil); var json ={"CoverageStartDate":"2021-08-15","CoverageEndDate":"2022-08-14","Status":"Submitted"}; var path = "$.CoverageStartDate"; JSONPathUtil.eval(json, path);
06-09-2021 09:13 AM
Can you try this?
importPackage(com.audium.server.cvpUtil); var json = JSON.parse('{"CoverageStartDate":"2021-08-15","CoverageEndDate":"2022-08-14","Status":"Submitted"}'); var path = "$.CoverageStartDate"; JSONPathUtil.eval(json, path);
david
06-10-2021 04:03 AM
Same result. It still thinks it's an array. When I print(json) it shows [object Object] in the console.
06-10-2021 04:36 AM
Chad,
My above example happened to be an array.
And that is why I have the [0] in the Path.
Give that a go?
Regards,
Gerry
06-14-2021 04:37 AM
It still fails, which was to be expected since this is not actually an array. Interesting though, when I do specify an array "path" it tells me the JSON is an object. Specifying an object and defining a path of the object gives me an array error while specifying an array path gives an error stating the variable is an object.
06-14-2021 04:52 AM
Finally! Here is the correct syntax for hard coding JSON strings:
importPackage(com.audium.server.cvpUtil); var json = '{"CoverageStartDate":"2021-08-15","CoverageEndDate":"2022-08-14","Status":"Submitted"}' var path = "$.CoverageStartDate"; JSONPathUtil.eval(json, path);
Hope this helps others!
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