Iterate through missing JSON element

Shantanu K
Tera Contributor

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

 

 

 

 

1 ACCEPTED SOLUTION

Dibyaratnam
Tera Sage

@Shantanu K ,

Inside the for loop, check 

if(purchaseItem.hasOwnProperty('invoice')){
gs.info('do your operation');
}

If this doesn't work, please send your sample JSON.

View solution in original post

2 REPLIES 2

Dibyaratnam
Tera Sage

@Shantanu K ,

Inside the for loop, check 

if(purchaseItem.hasOwnProperty('invoice')){
gs.info('do your operation');
}

If this doesn't work, please send your sample JSON.

Fazal Mohammad
ServiceNow Employee
ServiceNow Employee

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