'Use JSONPathUtil.eval to extract object information
I am working with data retrieved from API that is in JSON format. I am limited to using JSONPathUtil.eval as well. Given sample GET data I have the following output:
{
"ResponseSample":"{\"StartDate\":\"2005-08-01\",\"EndDate\":\"2008-09-31\",\"Status\":
\"Pending\"}"
}
The application I am using is Cisco Call Studio which uses preloaded Java elements. In the node to define variables I have the following:
importPackage(com.audium.server.cvpUtil);
var val= {Data.Element.GetUserCoverage.response_body}
var path = "$.StartDate";
JSONPathUtil.eval(val , path);
"var val" is the result of the API call above.
I am confused as the use of the backslash before and after the value:key pair as I understand those to be escape characters. Not sure if that is the cause of my issue or not. I have tried defining the following expressions to no avail:
$.StartDate
ResponseSample.*
ResponseSample.StartDate
In this instance does json.parse need to be used? Any feedback greatly appreciated. I am no pro to JSON so excuse the ignorance.
Solution 1:[1]
I have the same problem using postman where I configured the REST API setting for the GET method. Postman will display the JSON object with the backslashes. I suspect the backslashes are escape characters due to the double quotes. The JSON.parse() and JSON.stringify() within postman will not remove these backslashes. (e.g. used within Postman 'Tests')
I'm guessing you might be using a REST API and the JSON response comes back including the backslashes, as such the json response will need to be formatted into a valid json format.
Cisco Call Studio introduced a new utility to allow the use of JSONPath expressions. JSONPath is a query language for JSON. You can use JSONPath expressions when building your Cisco Call Studio. In the 'Settings' and for the 'value' for the 'name' you will use a java expression with the JSONPath expression.
Use the online tools for JSON and JSONPath expressions, which will help with building expressions for REST API calls when the response is using JSON.
So looking at the json sample, you will need to remove the backslashes. Once you have a valid json format, then use jsonpathfind.com to construct your desired path (JSONPath) expression.
Then you can validate your expression using https://jsonpath.com.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


