- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2023 10:13 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2023 10:36 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2023 10:36 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2023 02:02 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2023 02:10 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2023 03:40 AM
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"
}
}