Page 1 of 1

JSON web service

Posted: Wed Feb 26, 2014 9:34 pm
by jskibbie
I'm writing a ScriptElement that is using cURL to generate an HTTP POST to a web service. The response from the web service is a string representing a JSON object. I need to be able to parse the response to continue on with my script.



The response looks something like this:





{ "error": { "errors": [ { "domain": "usageLimits", "reason": "keyInvalid", "message": Bad Request } ], "code": 400, "message": "Bad Request" } };





It doesn't look like Switch has any native ability to work with this JSON string. What is the best/recommended way to be able to parse this JSON string? The best I can come up with is using 'eval', but that seems a bit scary to just run eval on arbitrary code returned from a web service.





var theResponse = '{ "error": { "errors": [ { "domain": "usageLimits", "reason": "keyInvalid", "message": Bad Request } ], "code": 400, "message": "Bad Request" } }';

var foo = eval('(' + theResponse + ')');

var errorCode = foo.error.code;

job.log (-1, errorCode );





Any thoughts?



Thanks.

Jim



JSON web service

Posted: Wed Feb 26, 2014 10:31 pm
by dkelly
You are correct that Switch doesn't have JSON functionality nor will any of the popular Javascript libraries work.



Many webservices support returning either JSON or XML. Does yours? If not, then you are left with basic string parsing of the returns JSON blob.

JSON web service

Posted: Wed Feb 26, 2014 11:01 pm
by jskibbie
Dwight-



Thanks for the quick reply. Unfortunately, the web service's only supported data format is JSON.



I'll be sending in my 'wish' request for some built-in JSON support to Enfocus. In the meantime, I guess I'll go with the eval method I've already tried out.



Thanks.

Jim