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.

How to parse JSON and insert in the table?

John_123_Snow
Mega Expert

I have the following array of objects

 [{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]

and need to insert insert 3 records to my table into fields, Device and Owner, how can I achieve this?

  DEVICE OWNER
RECORD1 Apple Joe Employee
RECORD2 Microsoft Jack Businessman
RECORD3 Huawei Patel Employee

 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Something like below. Replace "<table name>" with the name of table to insert into.

var jsonString = '[{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]';
var jsonObj = JSON.parse(jsonString);
for (var i=0;i<jsonObj.length;i++) {
  var device = jsonObj[i]['Device'];
  var owner = jsonObj[i]['Owner'];
  var gr = new GlideRecord("<table name>");
  gr.initialize();
  gr.device = device;
  gr.owner = owner;
  gr.insert();
}

 

EDIT:

Refer to following GlideRecord document on how to insert values.

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...

View solution in original post

3 REPLIES 3

Ct111
Tera Sage

Check this blog it will be helpful,

https://community.servicenow.com/community?id=community_blog&sys_id=aedbb9e8dbafbb805ed4a851ca9619ba

 

Post parsing you can glide to insert the record in table

https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_GlideRecordClientSideAPI

insert(Function responseFunction)

 

Mark my ANSWER as CORRECT and HELPFUL if it helps

Hitoshi Ozawa
Giga Sage
Giga Sage

Something like below. Replace "<table name>" with the name of table to insert into.

var jsonString = '[{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]';
var jsonObj = JSON.parse(jsonString);
for (var i=0;i<jsonObj.length;i++) {
  var device = jsonObj[i]['Device'];
  var owner = jsonObj[i]['Owner'];
  var gr = new GlideRecord("<table name>");
  gr.initialize();
  gr.device = device;
  gr.owner = owner;
  gr.insert();
}

 

EDIT:

Refer to following GlideRecord document on how to insert values.

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

sample script below

var json = '[{"Device":"Apple","Owner":"Joe Employee"},{"Device":"Microsoft","Owner":"Jack Businessman"},{"Device":"Huawei","Owner":"Patel Employee"}]';
var jsonObj = JSON.parse(json);
for (var i=0;i<jsonObj.length;i++) {
  var device = jsonObj[i].Device;
  var owner = jsonObj[i].Owner;
  var gr = new GlideRecord("table"); // give valid table name here
  gr.initialize();
  gr.setValue('field1',device); // give valid field name here
  gr.setValue('field2',owner); // give valid field name here
  gr.insert();
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader