Get Object from nested JSON

Erik29
Kilo Expert

Good Afternoon,

We need to call an REST API from the portal to request information in an external system for that we've created the following Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == 'true') {
        return;
    }
	
    var colleague = g_form.getValue("colleague");
	
    var gaj = new GlideAjax('generate_id');
    gaj.addParam('sysparm_name', 'getID');
	gaj.addParam('sysparm_user', colleague);
	gaj.getXMLAnswer(function(response){
		var myJSON = JSON.parse(response);	
		var ID = myJSON["PossibleUsernames[1].Username"];
		g_form.addInfoMessage(ID);
	
	});

 

As you can see this Client Script calls a Script Include called generate_id

This script include looks like this:

var generate_id = Class.create();
generate_id.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getID: function() {
var user = {};
var gr = GlideRecord("sys_user");
if (gr.get(this.getParameter("sysparm_user"))) {
user.firstName = gr.getValue("first_name");
user.lastName = gr.getValue("last_name");
user.mdmKey = gr.getValue("u_mdm_key");
}
var message = new sn_ws.RESTMessageV2("Username_v1", "/username/generate");
message.setStringParameterNoEscape("firstname", user.firstName);
message.setStringParameterNoEscape("lastname", user.lastName);
message.setStringParameterNoEscape("mdmkey", user.mdmKey);

var response = message.execute();
var responseBody = response.getBody();
return JSON.stringify(responseBody);
},

type: "generate_id"
});

 

The response we get in the return looks like the following:

{"PossibleUsernames":[{"Username":"pepig","Priority":1},{"Username":"peppi","Priority":2},{"Username":"peppp","Priority":3},{"Username":"peppa","Priority":4}]}

I'm looking for a way to extract the Usernames such as pepig, peppi, pepp, peppa and use the in my Catalog Client Script so i can use them in a g_form.addOption() or g_form.setValue(). 

 

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

Hi,

You can update client script like below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == 'true') {
        return;
    }
	
    var colleague = g_form.getValue("colleague");
	
    var gaj = new GlideAjax('generate_id');
    gaj.addParam('sysparm_name', 'getID');
	gaj.addParam('sysparm_user', colleague);
	gaj.getXMLAnswer(function(response){
		var myJSON = JSON.parse(response);
var userNames = myJSON.PossibleUsernames;

for(var i=0; i<userNames.length;i++){
g_form.addInfoMessage(userNames[i].Username);

//to add option use below logic
g_form.addOption('variable_name', userNames[i].Username,userNames[i].Username);

}
	
	});

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

9 REPLIES 9

Anil Lande
Kilo Patron

Hi,

You can update client script like below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == 'true') {
        return;
    }
	
    var colleague = g_form.getValue("colleague");
	
    var gaj = new GlideAjax('generate_id');
    gaj.addParam('sysparm_name', 'getID');
	gaj.addParam('sysparm_user', colleague);
	gaj.getXMLAnswer(function(response){
		var myJSON = JSON.parse(response);
var userNames = myJSON.PossibleUsernames;

for(var i=0; i<userNames.length;i++){
g_form.addInfoMessage(userNames[i].Username);

//to add option use below logic
g_form.addOption('variable_name', userNames[i].Username,userNames[i].Username);

}
	
	});

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi Anil, 

 

My new script will then be: 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == 'true') {
        return;
    }

    var colleague = g_form.getValue("colleague");

    var gaj = new GlideAjax('generate_id');
    gaj.addParam('sysparm_name', 'getID');
    gaj.addParam('sysparm_user', colleague);
    gaj.getXMLAnswer(function(response) {
        var myJSON = JSON.parse(response);
        var userNames = myJSON.PossibleUsernames;

        for (var i = 0; i < userNames.length; i++) {
         g_form.addInfoMessage(userNames[i].Username);

          //to add option use below logic
          g_form.addOption('proposed_user_id', userNames[i].Username, userNames[i].Username);
       }
    });
}

 

When i do this i get Unhandled exception in GlideAjax. Can you see what is wrong?

Hi,

Can you please put some logs in Script include and see what it is returning.

Also put Info message in Client script to check response.

 

If you receive correct response the these changes would work.

I tried returning your json from SI and printing the Names.

find_real_file.png

find_real_file.png 

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

For some reason my response is different this time it has slashes

 

"{\"PossibleUsernames\":[{\"Username\":\"pepig\",\"Priority\":1},{\"Username\":\"peppi\",\"Priority\":2},{\"Username\":\"peppp\",\"Priority\":3},{\"Username\":\"peppa\",\"Priority\":4}]}"