Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to create JSON array in following format.

Para5
Tera Guru

I have following requirement.

I want Json array such that :

 

"UserandRoles":[  

 

                      {  

 

                            "name":"Abel",

 

                            "role":"admin"

 

                      },

 

                      {  

 

                            "name":"Abel",

 

                            "role":"security_admin"

 

                      },

                       {  

 

                            "name":"John",

 

                            "role":"itil"

 

                      }

 

                ]

Script is as below:

var info = [];

var Information = {};

 

var gr = new GlideRecord('sys_users');

gr.addQuery('sys_id',  usersys);

gr.query();

if( gr.next()){

 

info.name = gr.name.getDisplayValue();
info.Roles = gr.u_roles.getDisplayValue(); // u_role field contain roles in folllowing format role1, role2, role3....

info .push(Information );

 

}

 

Thanks,

 

1 ACCEPTED SOLUTION

newhand
Mega Sage

@Para5 



var infoList = []
var Information = {};
var gr = new GlideRecord('sys_user');

gr.addQuery('sys_id', usersys);

gr.query();

if( gr.next()){


var info = {}
var user_name = gr.name.getDisplayValue();

var roles_arr = gr.roles.split(','); // u_role field contain roles in folllowing format role1, role2, role3....

for (var i = 0; i < roles_arr.length; ++i) {

info.name = user_name
info.role = roles_arr[i]
infoList.push(info);

}

}
Information.UserandRoles = infoList
gs.info(JSON.stringify(Information))

Please mark my answer as correct and helpful based on Impact.

View solution in original post

5 REPLIES 5

Vasantharajan N
Tera Sage
Tera Sage

Try the below code snippet

var gr = new GlideRecord('sys_users');
gr.addQuery('sys_id', usersys);
gr.query();

if (gr.next()) {
    var name = gr.name.getDisplayValue();
    var rolesArr = gr.u_roles.getDisplayValue().split(","); // u_role field contain roles in folllowing format role1, role2, role3....
    var userRole = {};
    var resultArr = [];
    if (rolesArr.length > 0) {
        for (var role = 0; role < rolesArr.length; role++) {
            userRole.role = rolesArr[role];
            userRole.name = name;
            resultArr.push(userRole);
        }
    }
    var finalResult = {
        "UserandRoles": resultArr
    };
    gs.info("Result " + finalResult);
}

Thanks & Regards,
Vasanth

newhand
Mega Sage

@Para5 



var infoList = []
var Information = {};
var gr = new GlideRecord('sys_user');

gr.addQuery('sys_id', usersys);

gr.query();

if( gr.next()){


var info = {}
var user_name = gr.name.getDisplayValue();

var roles_arr = gr.roles.split(','); // u_role field contain roles in folllowing format role1, role2, role3....

for (var i = 0; i < roles_arr.length; ++i) {

info.name = user_name
info.role = roles_arr[i]
infoList.push(info);

}

}
Information.UserandRoles = infoList
gs.info(JSON.stringify(Information))

Please mark my answer as correct and helpful based on Impact.

@newhand  
Its working but If user having 3 roles then only 1st role gets printed 3 times.

The 2nd and 3 rd role does not get printed


e.g.  abel tuter  (admin, security_admin, pa_reports)
 Abel tuter      admin

Abel tuter      admin

Abel tuter      admin

Jon   linkon        itil

try this.

 

info.role = roles_arr[i] + ""

Please mark my answer as correct and helpful based on Impact.