Created
July 2, 2015 14:57
-
-
Save bryanbarnard/3063766969d20edecdd9 to your computer and use it in GitHub Desktop.
Sample RESTMessageV2 Parse JSON Response
This file contains 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(); | |
} | |
})(); |
Glad it helped. It should work post Fuji, but you should check the API docs
at developer.servicen-now.com to verify and see any new features they have
added to the API.
…-Bryan
On Tue, Nov 12, 2019 at 2:42 AM Sailesh ***@***.***> wrote:
This is awesome! I'm sure it works for instances later Fuji too?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/3063766969d20edecdd9?email_source=notifications&email_token=AAHNLFPYPFHPOLYVRJ5SOY3QTKB7VA5CNFSM4JMBS56KYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF4BHA#gistcomment-3080816>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHNLFIUAUHWVSW7GGQIWITQTKB7VANCNFSM4JMBS56A>
.
--
Bryan Barnard
[email protected]
@bryanbarnard
missing ";" at 31
"gs.print('id: ' + parsed.results[i].id)"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome! I'm sure it works for instances later Fuji too?