JSON web service

Post Reply
jskibbie
Newbie
Posts: 10
Joined: Mon Jan 23, 2012 8:34 pm

JSON web service

Post 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


dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

JSON web service

Post 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.
jskibbie
Newbie
Posts: 10
Joined: Mon Jan 23, 2012 8:34 pm

JSON web service

Post 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
Post Reply