Need assistance parsing nested arrays into separate objects using Node.js. Assistance very much appreciated.
I need to not have these in final output-
"Status": "0",
"Message": "OK",
"Count": "3724",
AND
I need to have the following as separate json objects-
- InvoiceRecords (occurs one time in json)
- InvoiceRecordHeaderDetails (occurs multiple times in json)
- InvoiceRecordSplitDetails (occurs multiple times in json)
- InvoiceRecordLineItemDetails (occurs multiple times in json)
- InvoiceTaxCodeDetails (occurs multiple times in json)
Have the following json data sample (truncated):
{
"Status": "0",
"Message": "OK",
"Count": "3724",
"InvoiceRecords": [
{
"InvoiceDate": "8/9/2017 12:00:00 AM",
"InvoiceLocation": "002",
"InvoiceNumber": "2004085",
"InvoiceRecordHeaderDetails": [
{
"InvNum": "2004085",
"Location": "002",
"InvDate": "8/9/2017 12:00:00 AM"
}
],
"InvoiceRecordSplitDetails": [
{
"UniqueID": "1757391",
"InvNum": "2004085",
"Location": "002",
"InvoiceDate": "8/9/2017 12:00:00 AM"
}
],
"InvoiceRecordLineItemDetails": [
{
"UniqueID": "3939934",
"InvNum": "2004085",
"Location": "002",
"InvoiceDate": "8/9/2017 12:00:00 AM"
}
],
"InvoiceTaxCodeDetails": [
{
"InvoiceDate": "8/9/2017 12:00:00 AM",
"Location": "002",
"InvNum": "2004085",
}
]
}
]
}
Update 2
@chris-adorna
Following your instructions (I think), but hit a snag (help?)
// Parse input file data as json
var jsonContent = JSON.parse(fs.readFileSync(inputFile, 'utf8'));
//1. Create 4 empty arrays, one each for record header, splits, line items and tax code details
var recordHeader;
var splits;
var lineItems;
var taxCodes;
//2. Use a forEach function to iterate through InvoiceRecords
var i;
for (i = 0; i < jsonContent.length; i++) {
var record = jsonContent[i].InvoiceRecords;
};
console.log(record);
Step 2 not getting anything but 'undefined' in console.