Array of JSON objects

G Balaji
Kilo Guru

Hi,

How to initialize, add, remove objects to JSON array.

Scenario is below,

I've a scheduled job which will call a script include function which would return list of sys-id and field value of the record. 

Thanks.

7 REPLIES 7

Hello balaji

Check this script:

var books = [];
var bookTitle = "Tom Sawyer";

var author = "Mark Twain";

var foo = {};

foo[bookTitle] = author;

books.push(foo);

gs.log(JSON.stringify(books));

OUTPUT:

*** Script: [{"Tom Sawyer":"Mark Twain"}]

Please mark as Correct Answer and Helpful, if applicable.
Thanks!
Abhishek Gardade
Hexaware Technologies

Thank you,
Abhishek Gardade

Omkar Mone
Mega Sage

Hi Balaji,

 

var arr = {};

var arrMain = [];

 

arr.test1 = "ABC";

arr.test2 = "DEF";

arrMain.push(JSON.stringify(arr));

 gs.print(arrMain);

 

Try this in your background script its result will be- 

*** Script: {"test1":"ABC","test2":"DEF"}


Hope this helps too.

Regards,
Omkar Mone

amlanpal
Kilo Sage

Hi G Balaji,

 

As you are trying to create and use JSON Objects in your server side scripting, you may look into the below helpful links. These links contains the required examples to understand how to create and use the JSON arrays.

1. Advanced Scripting and JSON

2. JSON

 

Hope this helps. Please mark the answer Correct/Helpful based on the impact.

Regards,

Amlan