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

Have you made changes to script include?

 

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

This will be the result after stringify and you will receive it as response in client script.

Once you parse it in client script using JSON.parse(response) remaining logic should work as expected.

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

It's stuck on the last part 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == 'true') {
        return;
    }
	g_form.addInfoMessage("Running");
    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) {
		g_form.addInfoMessage(JSON.parse(response));
                g_form.addInfoMessage("IT IS WORKING UNTILL THIS POINT");
		
		// IT IS WORKING UNTILL THIS POINT
		
        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.setValue('proposed_user_id', userNames[i].Username);
       }
    });
}

 

find_real_file.png

It Was Stringify indeed in the SI

Glad to know your issue is resolved.

Happy learning 🙂

 

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