cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
10654
Views
37
Helpful
30
Replies

JSON Response from REST Client CVP 11.5

dmehta
Level 1
Level 1

Hi Team,

I am trying to make a REST call using Call studio REST Client to an endpoint.  However, in my json response, i get multiple values.

Although, i could see all the responses in the log, my requirement is to utilize only 1 response.

As per documentation, it looks like i can achieve the same outcome using  json expression but it doesnt work.

Example  my response id looks like this:

{

  "item": {

   "id": 123,

   "url": "https://{subdomain}.zendesk.com/api/v2/items/123.json",

   "name": "Wibble",

   ...

   "created_at": "2012-04-04T09:14:57Z"

  }

}

Now, i just need to store value of id in one of the session data. However, current response_body stores the entire response.

My xpath/json path looks like this :  /item/id


Please help.


30 Replies 30

ptindall
Cisco Employee
Cisco Employee

Set your path to $.item. and you should then be able to reference the data via Data.Element.<element-name>.id

If you only need the one field, then you could set the XPath in the Rest

Client element to $.item.id and you'll end with Element Data named 'id'

(it'll be named whatever the final thing is that you've asked for and is

most-likely case sensitive).

janinegraves
Spotlight
Spotlight

by the way, XPath uses slashes but JSON Path expressions use $ and dots.

I'm using the REST Client node as well with the REST call set in the Endpoint URL however I'm not seeing any responses in the log.  This is the response returned from Postman:

"[{\"CustomerId\":\"30268\",\"SubsidiaryId\":\"1\",\"CurrencyId\":null,\"CategoryId\":null,\"PriceLevelId\":null,\"TaxRegNumber\":null,\"CompanyName\":\"ABC Support Systems\",\"Email\":\"jode@support.com\",\"BusinessEmail\":\"jode@work.com\",\"Phone\":null,\"Accounts\":[{\"SiteId\":1,\"UserName\":\"30268\",\"Password\":\"12345\",\"IsForceReset\":false}],\"ExternalId\":null,\"IsResellerId\":null,\"Type\":null,\"InternalUser\":false,\"IsPerson\":false,\"InternalId\":1867,\"FirstName\":"john\",\"MiddleName\":null,\"LastName\":"doe\",\"DefaultAddress\":null}]"

I don't have anything sent in my Xpath/JSONPath.  Any ideas what I may be missing here?  Thanks

I should have added I'm trying to get the Email, and FirstName, LastName fields out of the reponse.

Also I am now seeing the response in the logs so just trying to figure out how to pull only those 3 fields.  Thanks

Hi Dave,

You'll have to use the SetValue element to parse the JSON response using JSONPath expressions.

1. Set Value element is in the Math folder in the Elements pane

2. In its Settings tab, right-click and choose Add Variable

3. Name it something like 'localEmail'  as it creates a new type of variable LocalVariable

4. In the value field, enter the following (case sensitive, use your rest client element data in the 2nd line)

importPackage(com.audium.server.cvpUtil);

var  val= {Data.Element.RestElementName.response_body};  (using your rest client element data)

var path = "$[0].Email";

JSONPathUtil.eval( val, path);

(Press OK)

5. To parse another item, right-click Add Variable: localFirstName

6. In the valuevfield, enter the following  (you don't need to include the first 2 lines from the previous code, as the javascript still remembers them.

var path = "$[0].FirstName";

JSONPathUtil.eval( val, path);

7. To parse another item, right-click Add Variable: localLaRName

8. In the valuevfield, enter the following:

var path = "$[0].LastName";

JSONPathUtil.eval( val, path);

Local Variables don't show up in the Debugger or the Activity Log - so you may want to AddToLog or assign them into Session Data for troubleshooting.

Janine

etc...

Thanks Janine.  Now I'm getting a different error when executing the Set Value element.  I created the local variable, localEmail.  In the value field I entered:

importPackage(com.audium.server.cvpUtil);

var val={Data.Element.SWIDRestCall.response_body};

var path = "$[0].Email";

JSONPathUtil.eval(val,path);

where SWIDRestCall is the name of my RestClient node.

The error log shows this:

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:05.779, 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.

    at com.audium.server.voiceElement.ActionElementBase.service(ActionElementBase.java:416)

    at com.audium.server.controller.Controller.goToAction(Controller.java:4363)

    at com.audium.server.controller.Controller.goToElement(Controller.java:3999)

    at com.audium.server.controller.Controller.continueCall(Controller.java:3679)

    at com.audium.server.controller.Controller.goToElement(Controller.java:4095)

    at com.audium.server.controller.Controller.continueCall(Controller.java:3679)

    at com.audium.server.controller.Controller.doPost(Controller.java:1099)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)

    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)

    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)

    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)

    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)

    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)

    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)

    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)

    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)

    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)

    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)

    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

    at java.lang.Thread.run(Thread.java:745)

Caused by: com.audium.server.AudiumException: Java Script Execution error

    at com.audium.server.action.setvalue.SetValueAction.executeJavaScript(SetValueAction.java:180)

    at com.audium.server.action.setvalue.SetValueAction.processLocalVariable(SetValueAction.java:142)

    at com.audium.server.action.setvalue.SetValueAction.doAction(SetValueAction.java:111)

    at com.audium.server.voiceElement.ActionElementBase.service(ActionElementBase.java:390)

Here's a snippet from the activity log shows this:

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:02.732,SWIDRestCall,enter,

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:05.764,SWIDRestCall,data,Body,"[{\"CustomerId\":\"39266\"\"SubsidiaryId\":\"1\"\"CurrencyId\":null\"CategoryId\":null\"PriceLevelId\":null\"TaxRegNumber\":null\"CompanyName\":\"Support Systems\"\"Email\":\"jdoe@support.com\"\"BusinessEmail\":\"jdoe@work.com\"\"Phone\":null\"Accounts\":[{\"SiteId\":1\"UserName\":\"39266\"\"Password\":\"2497\"\"IsForceReset\":false}]\"ExternalId\":null\"IsResellerId\":null\"Type\":null\"InternalUser\":false\"IsPerson\":false\"InternalId\":1867\"FirstName\":\"Joe\"\"MiddleName\":null\"LastName\":\"Doe\"\"DefaultAddress\":null}]"

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:05.764,SWIDRestCall,exit,done

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:05.764,Set Variables,enter,

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:05.779,Set Variables,custom,localEmail,sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.IllegalArgumentException: Invalid container object (<Unknown source>#4) in <Unknown source> at line number 4

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:05.779,Set Variables,exit,

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:05.779,,element,error,

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:13.529,,end,how,disconnect

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:13.529,,end,result,error

10.110.107.157.1516298695951.507369.TestSWIDLookup,01/18/2018 12:05:13.529,,end,duration,18

Any ideas on that?  Thanks!

Hi - This works on my cvp11 system so not positive where the problem is.

The error says there's a problem on line 4 of your javascript.

This counts blank lines as a line.

If you look at your Value box of your SetValue element - which line is

line 4?

If it's this line: var path = "$[0].Email";  then I would try removing

and replacing the quotes.

Let me know...

This works on my 11.0 and 11.5 systems (I copy/pasted YOUR javascript to be sure).


Are you sure you have the Rest client element name right?

Can you test this through your Studio Debugger? It might show you more info about the error and you could possibly add print line statements to display intermediate results. Let me know.

Try this.

1)Copy the json response into a text file and put it under your IIS C:\inetpub\wwwroot\test.json

2) Try to grab it in a web browser - http://hostnameOfVxmlServer/test.json

3) In your Rest Client element: copy the URL from the web browser:  http://hostnameOfVxmlServer/test.json

4) Copy the javascript into Notepad as a txt file and save it, to ensure there aren't any weird characters in your code.

Then remove what's in your SetValue and copy/paste from the txt file:

importPackage(com.audium.server.cvpUtil);

var val={Data.Element.SWIDRestCall.response_body};

var path = "$[0].Email";

JSONPathUtil.eval(val,path);

Thanks Janine. We got it working finally as you mentioned with this:

importPackage(com.audium.server.cvpUtil);

var  val= {Data.Element.RestElementName.response_body};  (using your rest client element data)

var path = "$[0].Email";

JSONPathUtil.eval( val, path);

It wasn't able to parse the field with the escape characters (\) in the json response.  Once we removed those everything worked. 

Hi,

I have a question similar to this. If I use Postman my response is in JSON format, like so:

We are doing a POST request with JSON format input.  I have a header set on the element of Content-Type:application/json

{

    "response": {

        "salesPlanAff": "100",

        "statusCd": "Active",

        "accountSubTypeCd": "BusinessOwner",

        "aboNum": "2303",

        "awardCd": "FEM",

        "awardDesc": "Founders Emerald"

    }

}

Using the REST Element in CVP 11.6 the response Body is HTML like this:

Is that the expected result?  I was expecting to get a JSON format back.

If this what we have to work with how can I parse that out in CVP studio without a custom element.

<BODY bgcolor=#dddddd>

<TABLE bgcolor=#dddddd border=1>

<TR>

<TD valign="top"><B>response</B></TD>

<TD><TABLE bgcolor=#dddddd border=1>

<TR>

<TD valign="top"><B>salesPlanAff</B></TD>

<TD>100</TD>

</TR>

<TR>

<TD valign="top"><B>statusCd</B></TD>

<TD>Active</TD>

</TR>

<TR>

<TD valign="top"><B>accountSubTypeCd</B></TD>

<TD>BusinessOwner</TD>

</TR>

<TR>

<TD valign="top"><B>aboNum</B></TD>

<TD>2303</TD>

</TR>

<TR>

<TD valign="top"><B>awardCd</B></TD>

<TD>FEM</TD>

</TR>

<TR>

<TD valign="top"><B>awardDesc</B></TD>

<TD>Founders Emerald</TD>

</TR>

</TABLE>

</TD>

</TR>

</TABLE>

</BODY>

You might be able to modify your query to the web service and explicitly request json format,instead of HTML.
Depending upon your web service, there might be a parameter (http://yourQuery?format=json) or part of the URI (http://youQuery/json).
Otherwise, you can use XPath to parse XML (HTML is XML) in the Set Value element.
     importPackage(com.audium.server.cvpUtil)
      XpathUtil.eval(xmlString,xpathStringExpression)

See w3schools.com for XPath tutorial - it's not too hard and there are free online tools (eg, freeformatter.com)  for you to test your XPath expressions on your XML/HTML to practice.

This screenshot shows a few XPath expressions to retrieve the first few items from the response. Note  that arrays in XPath begin at 1 (whereas JSONPath arrays begin at 0).

xmlXpath.JPG

Try using Accept header in the request set to application/json

Thanks Paul that gave me a JSON response, the doc on the CVP element spec guide is a little light for this element.

I assume that I can't directly reference the response however as {Data.Element.elementname.statusCd} and I will have to parse it all out still?

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: