-
-
Save nancystodd/f9ecc057dbff228f2d5da68a8ccc5a61 to your computer and use it in GitHub Desktop.
Sample RESTMessageV2 Parse JSON Response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: this sample was run in a ServiceNow instance running release version Fuji Patch 4 | |
(function sampleRequest() { | |
try { | |
var request = new sn_ws.RESTMessageV2(); | |
request.setHttpMethod('get'); | |
request.setEndpoint('https://rawgit.com/anonymous/f1c4664cafd6caf855fd/raw/f7ca3b022045e2c9f49dd2b453dea0cecc09888c/sample_json.json'); | |
var response = request.execute(); | |
var httpResponseStatus = response.getStatusCode(); | |
var httpResponseContentType = response.getHeader('Content-Type'); | |
var parser = new JSONParser(); | |
var parsed = {}; | |
var httpResponseBody; | |
gs.print("http response status_code: " + httpResponseStatus); | |
gs.print("http response content-type: " + httpResponseContentType); | |
// if request is successful then parse the response body | |
if (httpResponseStatus == 200 && httpResponseContentType == 'application/json;charset=utf-8') { | |
httpResponseBody = response.getBody(); | |
// parse JSON string returned from request into a json object | |
parsed = parser.parse(httpResponseBody); | |
// iterate over JSON object only printing the id property of JSON objects in results array | |
for (var i = 0; i < parsed.results.length; i++) { | |
gs.print('id: ' + parsed.results[i].id) | |
} | |
} | |
} | |
catch (ex) { | |
var message = ex.getMessage(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment