- 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-17-2023 03:49 AM
you can check if json contains key or not
Check if a key exists inside a JSON object
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:49 PM
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