JSON web service
Posted: Wed Feb 26, 2014 9:34 pm
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
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