cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
6157
Views
26
Helpful
9
Replies

Parse JSON object in CVP

chad_meyer
Level 1
Level 1

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.

1 Accepted Solution

Accepted Solutions

chad_meyer
Level 1
Level 1

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!

View solution in original post

9 Replies 9

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/weekDaySchedule1/dayOfWeek"
dow = XpathUtil.eval(xml,dowPath)
david

Gerry O'Rourke
Spotlight
Spotlight

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);

JSON-SetValue.png

Good catch, didn't even realize it was XML. +5

 

david

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);

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

Same result.  It still thinks it's an array.  When I print(json) it shows [object Object] in the console.

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

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.

chad_meyer
Level 1
Level 1

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!