0

How can I get AccountNo and InvoiceAmount by using for loop from this JSON array in JavaScript :

[
  {
    "Billinfo": "{\"BillInfo\":[{\"Accountno\":3,\"BillCycleNo\":1,\"Bill StartDate\":\"28-May-2021 00:00:00\",\"Bill EndDate\":\"14-Jun-2021 00:00:00\",\"Previous Bill StartDate\":\"28-May-2021 00:00:00\",\"Previous Bill EndDate\":\"14-Jun-2021 00:00:00\",\"InvoiceNo\":\"INV00000001000000000320210621175559\",\"InvoiceId\":\"210600000011\",\"SubscriberType\":\"ADVANCEPAID\",\"DueDate\":null,\"InvoiceAmount\":0.0,\"BillLines\":null,\"PenaltyCalculation\":null,\"InvoiceLevelCalculation\":null,\"InvoiceSummary\":null}]}"
  },
  {
    "Billinfo": "{\"BillInfo\":[{\"Accountno\":3,\"BillCycleNo\":2,\"Bill StartDate\":\"15-Jun-2021 00:00:00\",\"Bill EndDate\":\"14-Jul-2021 00:00:00\",\"Previous Bill StartDate\":\"28-May-2021 00:00:00\",\"Previous Bill EndDate\":\"14-Jun-2021 00:00:00\",\"InvoiceNo\":\"INV00000002000000000320210621181409\",\"InvoiceId\":\"210600000015\",\"SubscriberType\":\"ADVANCEPAID\",\"DueDate\":null,\"InvoiceAmount\":0.0,\"BillLines\":null,\"PenaltyCalculation\":null,\"InvoiceLevelCalculation\":null,\"InvoiceSummary\":null}]}"
  }
]
robdev91
  • 1,006
  • 1
  • 10
  • 18
Abjava
  • 11
  • 2
  • The site has a design with appropriate font weights, etc. please don't put your entire question in boldface. – T.J. Crowder Jul 09 '21 at 09:37
  • You need to look up `JSON.parse` as the `data[0].Billinfo` is not JSON as you state, but a string that needs to be decoded / parsed before you can continue on... – Djave Jul 09 '21 at 09:40
  • Assuming you're really receiving what you've shown as JSON text (e.g., a **string** in JavaScript), you need to parse it via `JSON.parse`. Unfortunately, whatever produced that JSON double-encoded the values in the `Billinfo` property, so once you've parsed the array, the objects in the array will have `Billinfo` properties with string values and you have to parse them a *second* time to get the data from it. For example: `const data = JSON.parse(theJSON); for (const obj of data) { obj.Billifo = JSON.parse(obj.Billinfo); }` Now it's all just objects and arrays. – T.J. Crowder Jul 09 '21 at 09:48
  • @T.J.Crowder I did this and printed to see if it works but it is showing an error that the JSON is returning undefined. – Abjava Jul 09 '21 at 10:08

0 Answers0