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

Varsha21
Giga Guru

Hi,

could you please tell me what your script is doing?

why the 2 arrays ?

and tell what values you want to push into Json?

 

 

Hi Varsha,

 

I have added script in post. I need all active fields and their value in jSON as response of scripted API. We need all fields in response from table and table and sys_id will be given dynamically. 

Basically, we are returning all affected CIs from Incident in Incident status update API.

E.g. If Incident has 1 database and one server CI as affected CI, so we expect Incident status updates and both affected CIs in response. So I am trying to build JSON with affected CI in Incident status response.

 

Hope this gives you some idea of what I am trying to achieve. If another way to achieve this, please let me know.

Hi,

use like this,

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],gr.getDisplayValue(fieldList[i]));        
//field_name array2.push();      //field_value
//array3.push(fieldList[i] , gr.getDisplayValue(fieldList[i]));
//array3.push(array1,array2);
}
//gs.print(array1);

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

you ll get expected result.

 

varsha

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.