- 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-03-2017 11:14 PM
Hi Chris,
Can you provide that gigantic JSON object and let me know which values you are looking for? And to parse a JSON, you can just use JSON.parse(JSONValue)
Thanks and Regards,
Aswin Siddalingam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2017 08:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2017 10:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2017 06:37 AM
Using Chuck's approach, it didn't work. Thanks for the link though!
Chris