On JSON payload get child object

Pavan_Snow_1
Kilo Guru

Hi all,

I have question on Integration. I am using Scripted REST API to consume the data on ServiceNow system.

Below is the sample payload. I am able to get the individual attributes like Name, country etc and able to map with target fields on ServiceNow. I need the entire Contact info object in one attribute. It has email and businessunit fields need to capture in single field. How to get the Contact info object?

 

{
    "BusinessAccount": {
        "PrimaryAccount": {
            "Name": "test",
            "country": "GB",
            "Contact info": {
                "email": "test@gmail.com",
                "businessUnit": "GB01"
            }
        }
    }
}

 

 

Thanks,

Pavan

1 ACCEPTED SOLUTION

Hi,

are you saying you want to store the json string into some string field?

if yes then do this

var json = '{"BusinessAccount":{"PrimaryAccount":{"Name":"test","country":"GB","Contact info":{"email":"test@gmail.com","businessUnit":"GB01"}}}}';

var parser = JSON.parse(json);

var contactInfo = JSON.stringify(parser.BusinessAccount.PrimaryAccount["Contact info"]);

gs.info(contactInfo);

Output:

find_real_file.png

Regards
Ankur

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

contact Info object has 2 information within it i.e. email and businessUnit

So you can get those individually but you need to decide which target field of the table will hold that information

regards
Ankur

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

Hi,

sample script to fetch the details

var json = '{"BusinessAccount":{"PrimaryAccount":{"Name":"test","country":"GB","Contact info":{"email":"test@gmail.com","businessUnit":"GB01"}}}}';

var parser = JSON.parse(json);

var contactEmail = parser.BusinessAccount.PrimaryAccount['Contact info'].email;

var contactBusinessUnit = parser.BusinessAccount.PrimaryAccount['Contact info'].businessUnit;

gs.info(contactEmail);
gs.info(contactBusinessUnit);

Output:

find_real_file.png

Regards
Ankur

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

Hi,

Individual attributes I can get. I need to get the entire object of "Contact info" as given below. both email and businessunit fields need to stored in a single variable.

"Contact info": {
                "email": "test@gmail.com",
                "businessUnit": "GB01"
            }

 

Thanks,

Pavan

Hi,

are you saying you want to store the json string into some string field?

if yes then do this

var json = '{"BusinessAccount":{"PrimaryAccount":{"Name":"test","country":"GB","Contact info":{"email":"test@gmail.com","businessUnit":"GB01"}}}}';

var parser = JSON.parse(json);

var contactInfo = JSON.stringify(parser.BusinessAccount.PrimaryAccount["Contact info"]);

gs.info(contactInfo);

Output:

find_real_file.png

Regards
Ankur

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