- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 11:20 PM
Hello Experts,
I am iterating through JSON response, where response looks like:
purchaseorder[array]
line items[array]
invoice[array]
invoice Line Items[array]
For some purchase items Invoice array is completely missing and my for loop is stuck! No getting undefined or null value in log.
How can I continue to iterate in such condition?
Thanks,
Shantanu
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 11:47 PM
Inside the for loop, check
if(purchaseItem.hasOwnProperty('invoice')){
gs.info('do your operation');
}If this doesn't work, please send your sample JSON.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 11:47 PM
Inside the for loop, check
if(purchaseItem.hasOwnProperty('invoice')){
gs.info('do your operation');
}If this doesn't work, please send your sample JSON.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2023 01:02 AM
Hi @Shantanu K
Please check below code:
for(var i in purchaseOrders){
var purchaseItem = purchaseOrders[i];
if(purchaseItem.Invoices && purchaseItem.Invoices.length > 0){
for(var j in purchaseItem.Invoices){
var invoice = purchaseItem.Invoices[j];
//write you're code here
}
}
//you can write else condition to handle the case, when Invoices array is not being send as part of response
}
Please mark the answer correct and helpful, if answered you're query.
Thanks
Fazal