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

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

Hi @Ankur Bawiskar ,

Thanks Ankur that will work I missed this approach.  

Do we have any method to check whether object is empty or not?

I tried as below but looking for best method to validate whether object is empty or not.

var charobj = request.body.data.Main.Childs;

if it is not empty then i am getting [object Object] else I am getting undefined.

 

@Pavan_Snow_1 

there are many ways to know if json body is empty

also if there are no childs then the length will be 0

you can use that to check at the beginning.

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

Hi @Ankur Bawiskar 

length only we can get when Childs object is part of Payload

If we receive payload as below then trying to validate see below payload.

There is no childs then no need to create records. Trying validate this.

{
    "Main": {
        "Name": "parent 1",
        "Number": "887342"
    }
}