Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Convert 2d array into JSON Object

Kiran Patil3
Giga Expert

Hi All,

I have 2d array which contains field_name and field_value in respective array object. I want convert it into JSON object like

{ "field_name" : "field_value" }

Here is code I have tried so far:

var gr = new GlideRecord('sys_user');
gr.addQuery('user_name','admin');
gr.query();
var array1 = [];
var array2 = [];
var array3=[];
var response = {};
if(gr.next())
{

var gRU = new GlideRecordUtil();
var fieldList = gRU.getFields(gr);
//gs.print(fieldList);
for(i=0;i<fieldList.length;i++)
{
array1.push(fieldList[i]);                          //field_name
array2.push(gr.getDisplayValue(fieldList[i]));      //field_value
//array3.push(fieldList[i] , gr.getDisplayValue(fieldList[i]));
//array3.push(array1,array2);
}gs.print(array3.push(array1,array2));

}gs.print(global.JSON.stringify(array3));

Here is output: I tried to convert these value in JSON object but its not working. Can anyone help here?

*** Script: 2
*** Script: [["sys_id","country","calendar_integration","user_password","last_login_time","last_login_device","source","sys_updated_on","building","web_service_access_only","notification","sys_updated_by","enable_multifactor_authn","sys_created_on","sys_domain","state","vip","sys_created_by","zip","home_phone","time_format","accumulated_roles","last_login","default_perspective","active","time_sheet_policy","average_daily_fte","last_password","sys_domain_path","phone","cost_center","name","employee_number","password_needs_reset","gender","city","user_name","failed_attempts","edu_status","roles","title","sys_class_name","internal_integration_user","ldap_server","mobile_phone","street","company","department","first_name","preferred_language","introduction","email","manager","locked_out","sys_mod_count","last_name","photo","avatar","middle_name","time_zone","sys_tags","schedule","date_format","location"],["6816f79cc0a8016401c5a33be04be441",null,"Outlook","********","2019-03-19 09:48:41","8.36.116.206","","2019-03-18 17:09:20","","false","Enable","system","false","2007-07-03 11:48:47","global","","false","fred.luddy","","",null,"START,admin,END","2019-03-19","","true","","","","/","","","System Administrator","","false",null,"","admin","0",null,"admin","System Administrator","User","false","","","","","Finance","System",null,null,"admin@example.com","","false","99","Administrator","","c148e1d13741310042106710ce41f149.iix","",null,"","",null,""]]

 

Thank You

Kiran

1 ACCEPTED SOLUTION

Harish Murikina
Tera Guru

Hi Kiran,

Here you go , please mark it as correct answer if this works for you.

 

var resp = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', 'admin');
gr.query();
if (gr.next()) {
var gRU = new GlideRecordUtil();
var fieldList = gRU.getFields(gr);
var user = {};

fieldList.forEach(function(element) {

user[element] = gr.getDisplayValue(element)
});
resp.push(user);
}
gs.info(JSON.stringify(resp));

 

It worked for me, please see below screen shot.

 

find_real_file.png

 

Regards,

Harish Murikinati.

View solution in original post

6 REPLIES 6

Kiran Patil3
Giga Expert

Thanks Harish,

 

This is what I was looking for. 🙂

Nice to hear , please do not use for loop , always use forEach . Follow functional programming which will reduce writting number of lines and functional programming is faster. Please refer this link to know how functional programming will effective 

Regards,

Harish Murikinati.