How to save data in JSON format

Priya75
Tera Contributor

Hi All,

 

I am quering  a table and want to save this record , so my format looks like this .

so first one is the Sys_id and it has more than 1 record associated with it. which i am getting from a table. how can have a Json for this  and then how can i fetch values from this.

{ "1233898823190" : ["123", "234", "476879"]}

4 REPLIES 4

Vaibhav Dane
Tera Expert
Tera Expert

Hi Priya,

 

You can use below logic to prepare your JSON.

 

var incGR = new GlideRecord('incident');
incGR.setLimit('5');
incGR.query();
var obj3 = {};
while(incGR.next()){
var obj2 = {};
obj2.number = incGR.number.toString();
obj2.description = incGR.description.toString();
var sys = incGR.sys_id;
obj3[sys] = obj2;
}
gs.print('obj2 '+JSON.stringify(obj3));

 

Please mark correct if helpful.

Bhaba
Tera Expert

Hi Priya,

Here's one detailed article which explains how to store and fetch data in JSON: Store & Fetch Value from JSON

 

Hope this helps you.

Thanks

SumanthDosapati
Mega Sage
Mega Sage

Hi,

You can use below format

{
 "sysid1" : "123, 234, 4567",
"sysid2" : "345, 678"
}
//similarly for others

And to fetch the values

var prop = gs.getProperty("property_name").toString();

var sysid1 = prop.sysid1;
var first_array = sysid1.split(',');
first_array[0]; //to get 123
first_array[1]; //to get 234

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

Feel free to reach out if you have further questions or else you can mark an answer as correct and helpful to close the thread so that it benefits future visitors also.

Regards,
Sumanth