cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
134
Views
0
Helpful
0
Replies
Highlighted

Parsing REST response with JSONPathUtil in Set Value

JSONPathUtil.eval(inputJSON, expression) not working for me.

 

My attempts to use JavaScript in the Set Value element to find values within JSON from a web service have been unsuccessful.  My development IDE is Call Studio 11.5(1) and I'm running a test app Call Studio's debugger.

 

The test app is pretty simple:

JSON Parse Test AppJSON Parse Test App

This test app downloads JSON from a simple test site running in Visual Studio.

 

With the Call Studio debugger paused on the last Action element, selecting the REST Client element shows the response_body data is successfully downloaded from the web site:

{"ErrorMsg":null,"Patients":[{"NameComponents":{"FirstName":"David"}}]}

Debugger response_bodyDebugger response_body

I have created a Setting named "FirstName" on the Set Value element.  The "Response" setting is only to show me the JSON data that the JavaScript is working with.  The values of both settings are copied to Session variables on the Data tab to make them easier to see and log.

SetValueFirstName.png

The JavaScript in the FirstName setting is:

importPackage(com.audium.server.cvpUtil);
var inputJSON = {Data.Element.Rest_Client_01.response_body};
var expression = "$.Patients[0].NameComponents.FirstName";
JSONPathUtil.eval(inputJSON, expression);

 

The "expression" passed to JSONPathUtil.eval produces errors at the Set Value element:

SetValue_Excpt.png

The test application's error log contains corresponding info:

The error was: A built-in element encountered an exception of type com.audium.server.AudiumException. Java Script Execution error The root cause was: javax.script.ScriptException: sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.IllegalArgumentException: Invalid container object (<Unknown source>#4) in <Unknown source> at line number 4
com.audium.server.AudiumException: A built-in element encountered an exception of type com.audium.server.AudiumException.

 

If I change the "expression" to simply "$", then JSONPathUtil.eval will return the entire JSON response.

SetValue_$.png

 

I have tried many expressions to get any part of the JSON data but have been unsuccessful.  I'd like to;

1. get the value for FirstName

2. get the value for ErrorMsg

2. count the number of Patients returned in the Patients array (there is only one in this test data)

Any ideas to fix my search expressions or code would be appreciated.  Thanks!

Everyone's tags (5)