The CreatorCon Call for Content is officially open! Get started here.

Get Multiple Values from a REST Response

ctsmith
Mega Sage

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.

1 ACCEPTED SOLUTION

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.


View solution in original post

6 REPLIES 6

aswinsiddaling2
Kilo Guru

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


Here's a sample attached from Apperian.   I'm not going to include one from our Apperian instance as   I can't judge if there is sensitive information or not in the JSON that they wouldn't want me to expose.



Let's just say I wanted all the "name" in this JSON?



Thanks!


Using Chuck's approach, it didn't work.   Thanks for the link though!



Chris