-1

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.

Jason Gregory
  • 139
  • 1
  • 16
  • When you say you need those things that occur multiple times as separate objects, do you mean as separate arrays of objects? Also, do they occur multiple times in the sense that each is an array already in your input, or in the sense that `InvoiceRecords` is an array so could contain multiple objects each with its own `InvoiceRecordHeaderDetails` array? (By the way, [there ain't no such thing as a "json object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/).) – nnnnnn Oct 05 '17 at 02:14
  • Sorry, I'm very new at this. The `InvoiceRecords` array is a list of various invoices. A given invoice will have 1 header, 1 or more splits (split the bill across payers), 1 or more line Items (multiple products sold), and 1 or more tax code details (tax for each line item) – Jason Gregory Oct 05 '17 at 02:36
  • I think you need to show what the desired output is supposed to look like as that is not clear to me from your words. – jfriend00 Oct 05 '17 at 03:02
  • I’d like to output each array to individual json files containing just the contents of the array. The record header in a file, the splits in another file, the line items in another file, and the tax code details in another file. – Jason Gregory Oct 05 '17 at 03:13

1 Answers1

0

Taking your question literally, especially this part: "output each array to individual json files", try something along the lines of:

  1. Create 4 empty arrays, one each for record header, splits, line items and tax code details
  2. Use a forEach function to iterate through InvoiceRecords
  3. For each iteratee object, use a for...of statement to iterate through the Object.entries
  4. Using a conditional expression, if the key === your target (i.e. 'InvoiceTaxCodeDetails' ), pushthe value into the appropriate array
  5. Once the forEach function has completed, thenuse the native fs module to write the contents of each array to a JSON file
  • See "Update 2" above. – Jason Gregory Oct 06 '17 at 02:47
  • `var record` is defined inside your `for` loop. Therefore `record` in `console.log(record);` will be `undefined` as you are calling it outside the loop. This is part of JS' scoping rules. On a related not, `var i` should be declared inside your for parentheses like `for (var i`. If not, `i` will be scoped globally in your script and you may/will encounter all sorts of side effects. – Chris Adorna Oct 06 '17 at 03:36
  • Also, you cannot use a standard `for` loop on an object in the way that you are intending. It's used for arrays. Which is why I suggested a `forEach` function as that will iterate an object. I provided a link in my answer to help you understand how `forEach` works. You can also do a [for(in) loop](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object). – Chris Adorna Oct 06 '17 at 03:41