The CreatorCon Call for Content is officially open! Get started here.

How to import data into Sn from JSON.

Reddymahesh
Tera Contributor

Hi,

How to import a data into ServiceNow from JSON. This process should create new import set table and load it with data from JSON and transform the data into target table.

 

Thank you for your responses.

1 REPLY 1

Marco0o1
Tera Sage

Hi @Reddymahesh ,

 

If you have the Json object with a good structure i can recomend to you to just run a Background Script and create your records, I give you a brief example using the sys_user table to insert some users usin the Background Script:

 

// Here would be your array of object containing the information, users is a example

var users = [
  {
    user_name: 'alice.smith',
    first_name: 'Alice',
    last_name: 'Smith',
    email: 'alice.smith@example.com',
    mobile_phone: '+1234567890',
    department: '8de5d4c55c9211107f44347096fdb47a'
  },
  {
    user_name: 'bob.jones',
    first_name: 'Bob',
    last_name: 'Jones',
    email: 'bob.jones@example.com',
    mobile_phone: '+9876543210',
    department: '7ab8e1d42f8d26a95c4e6ff5c7410e1'
  },
  {
    user_name: 'charlie.wilson',
    first_name: 'Charlie',
    last_name: 'Wilson',
    email: 'charlie.wilson@example.com',
    mobile_phone: '+1555123456',
    department: '2b1c5e8e0a5d4f42f7d6d7c19a8b2a3'
  },
  {
    user_name: 'david.martin',
    first_name: 'David',
    last_name: 'Martin',
    email: 'david.martin@example.com',
    mobile_phone: '+18887776666',
    department: 'e9d3e5a1e1e0d4d2f7d7c4e6e2f6d7c'
  },
  {
    user_name: 'emily.davis',
    first_name: 'Emily',
    last_name: 'Davis',
    email: 'emily.davis@example.com',
    mobile_phone: '+1122334455',
    department: 'a1b2c3d4e5f6g7h8i9j0k1l2m3n4'
  },
  {
    user_name: 'frank.thompson',
    first_name: 'Frank',
    last_name: 'Thompson',
    email: 'frank.thompson@example.com',
    mobile_phone: '+9988776655',
    department: 'z9y8x7w6v5u4t3s2r1q0p'
  },
  {
    user_name: 'grace.walker',
    first_name: 'Grace',
    last_name: 'Walker',
    email: 'grace.walker@example.com',
    mobile_phone: '+1122334455',
    department: 'aabbccddeeffgghhiijjkkllmmnnoopp'
  },
  {
    user_name: 'henry.white',
    first_name: 'Henry',
    last_name: 'White',
    email: 'henry.white@example.com',
    mobile_phone: '+9988776655',
    department: '11223344556677889900'
  },
  {
    user_name: 'isabella.martinez',
    first_name: 'Isabella',
    last_name: 'Martinez',
    email: 'isabella.martinez@example.com',
    mobile_phone: '+1122334455',
    department: 'abcde12345fghij67890'
  },
  {
    user_name: 'jackson.johnson',
    first_name: 'Jackson',
    last_name: 'Johnson',
    email: 'jackson.johnson@example.com',
    mobile_phone: '+9988776655',
    department: 'xyz1234567890abcde'
  }
];

//initialize
var userGR = new GlideRecord("sys_user"); //There you would target the desire table

userGR.initialize();

for (var i = 0; i < users.length ; i++){
  //Then you would map manually your object name with the field name of the target table and then inser.
    userGR.user_name = users[i].user_name;
    userGR.first_name = users[i].first_name;
    userGR.last_name = users[i].last_namee;
    userGR.email = users[i].email;
    userGR.mobile_phone = users[i].mobile_phone;
    userGR.department = users[i].department;
    var userID = userGR.insert();
    if(!userID){
        //throw a error
    }
}

 

That must create all your record in a row if your data is correctly created, you can add validations if you want. 

 

Hope that help you.