- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2017 03:23 PM
I have a script include that runs when an item is loaded.
The catalog client script is:
function onLoad() {
//This works
var ga = new GlideAjax('apperianRest');
ga.addParam('sysparm_name','RestFun');
//ga.addParam('sysparm_user_name',"Bob");
ga.getXML(apperianRestParse);
function apperianRestParse(response) {
}
}
The script include is:
var apperianRest = Class.create();
apperianRest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//initialize :function(){},
RestFun :function(){//Put function code here},
try {
var r = new sn_ws.RESTMessageV2('Apperian', 'post');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
result = responseBody;
var parser = new JSONParser();
var parsed = parser.parse(result); // Pass the response object here
var token = parsed.token;
}
catch(ex) {
var message = ex.getMessage();
}
RestFun1();
function RestFun1(){
try {
var r1 = new sn_ws.RESTMessageV2('Apperian', 'get');
r1.setRequestHeader('X-TOKEN', token);
gs.log("something? " + token);
var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();
result = responseBody1;
gs.log("Giggity " + result);
}
catch(ex) {
var message = ex.getMessage();
}
}
},
type: 'apperianRest'
});
This works. But, I'm stuck. First, I read on the wiki that JSon.parser is deprecated. Is that true? Is it bad to use this? Should I use something else?
The next function RestFun1 works as well. When I gs.log the result of this function, there is a gigantic object returned from the REST call. I just want certain values from it. How do I pick through this JSon object and get the values I want. The trick here is that the values share the same field name many, many times. For instance:
"app_name" is listed about a 100 times with different values.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2017 07:48 AM
I was starting with the app names. I finally got this to work with the help of a coworker (and of course this thread). I did a combination of things, and it seems I wasn't looping through all the way. Just going through parsed.applications.app_name did work. And not using JSON.parse(result) first but using the loop didn't work either. This worked:
try {
var r1 = new sn_ws.RESTMessageV2('Apperian', 'get');
r1.setRequestHeader('X-TOKEN', token);
gs.log("something? " + token);
var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();
result = responseBody1;
var parsed = JSON.parse(result);
for (var i = 0; i <= parsed.applications.length; i++) {
gs.log("Here you go " + parsed.applications[i].version.app_name);
}
The result was a list of the applications.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2017 07:00 AM
Hi Chris,
You need to use JSONParser() if you are not using scoped app.
If you are using scoped app JSON.decode() would work for you
For non-scoped app
var responseBody1 = response1.getBody();
var parser = new JSONParser();
var parsedData = parser.parse(responseBody1);
OR
For scoped-app
var responseBody1 = response1.getBody();
var parser = new global.JSON();
var parsedData = parser.decode(responseBody1);
you can then parse and fetch whatever value you want.
Can you please let me know what values you want to retrieve from that sample json you shared?
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2017 07:48 AM
I was starting with the app names. I finally got this to work with the help of a coworker (and of course this thread). I did a combination of things, and it seems I wasn't looping through all the way. Just going through parsed.applications.app_name did work. And not using JSON.parse(result) first but using the loop didn't work either. This worked:
try {
var r1 = new sn_ws.RESTMessageV2('Apperian', 'get');
r1.setRequestHeader('X-TOKEN', token);
gs.log("something? " + token);
var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();
result = responseBody1;
var parsed = JSON.parse(result);
for (var i = 0; i <= parsed.applications.length; i++) {
gs.log("Here you go " + parsed.applications[i].version.app_name);
}
The result was a list of the applications.