Need to get the complete in the payload

Pavan_Snow_1
Kilo Guru

I have the scripted REST API and trying to get the array of objects from the below payload.

It has parent and child data I need to get the all the list of child and need to iterate that to create new records but I am not able to get the child data as array.

{
    "Main": {
        "Name": "parent 1",
        "Number": "887342",
        "Childs": {
            "childs": [
                {
                    "Name": "Child 1",
                    "id": "12345"
                },
                {
                    "Name": "Child 2",
                    "id": "15423"
                }
            ]
        }
    }
}

If i need to get the Name of first array then I am able to access as below.

var name = request.body.data.Main.Childs.childs[0].Name;

 But I am looking to get all the childs in single array.

Thanks in advance

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Pavan_Snow_1 

so you want to create n records if n childs are there in json?

if yes then why not create records during the iteration itself

like this

var jsonString = request.body.dataString;

var parseData = JSON.parse(jsonString);

for(var i=0;i<parseData.Main.Childs.childs.length;i++){

var name = parseData.Main.Childs.childs[i].Name;

var id = parseData.Main.Childs.childs[i].id;

// glide record here

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

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

View solution in original post

6 REPLIES 6

@Pavan_Snow_1 

you can check if json contains key or not

Check if a key exists inside a JSON object 

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

Pradyumna Das
Tera Expert

@Pavan_Snow_1 

Please use the below script and let me know if you need any help more.

var arrayData= request.body.data.Main.Childs.childs; //It will store the array

for(var i=0;i<arrayData.length;i++){//iterate startes from here
var tempName = arrayData[i].Name.toString();
var tempID = arrayData[i].id.toString();
gs.info('Array ['+i+'] --  Name : '+tempName+'  ID: '+tempID); //Print the Name and ID
}

 

Please mark it correct if my response helped you.

Thank You,

Pradyumna Das