简体   繁体   English

使用 Node.js 将嵌套的 json 数组解析为单独的 json 对象

[英]Parse Nested json Array into Separate json Objects with Node.js

Need assistance parsing nested arrays into separate objects using Node.js.需要帮助使用 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-我需要将以下内容作为单独的 json 对象-

  • InvoiceRecords (occurs one time in json) InvoiceRecords(在 json 中出现一次)
  • InvoiceRecordHeaderDetails (occurs multiple times in json) InvoiceRecordHeaderDetails(在json中多次出现)
  • InvoiceRecordSplitDetails (occurs multiple times in json) InvoiceRecordSplitDetails(在 json 中多次出现)
  • InvoiceRecordLineItemDetails (occurs multiple times in json) InvoiceRecordLineItemDetails(在 json 中多次出现)
  • InvoiceTaxCodeDetails (occurs multiple times in json) InvoiceTaxCodeDetails(在 json 中多次出现)

Have the following json data sample (truncated):有以下json数据样本(截断):

{
"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更新 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.第 2 步在控制台中除了“未定义”之外什么都没有。

Taking your question literally, especially this part: "output each array to individual json files", try something along the lines of:从字面上看你的问题,尤其是这部分:“将每个数组输出到单独的 json 文件”,尝试一些类似的东西:

  1. Create 4 empty arrays, one each for record header, splits, line items and tax code details创建 4 个空数组,记录标题、拆分、行项目和税码详细信息各一个
  2. Use a forEach function to iterate through InvoiceRecords使用forEach 函数遍历InvoiceRecords
  3. For each iteratee object, use a for...of statement to iterate through the Object.entries对于每个 iteratee 对象,使用for...of 语句遍历Object.entries
  4. Using a conditional expression, if the key === your target (ie 'InvoiceTaxCodeDetails' ), push the value into the appropriate array使用条件表达式,如果键 === 您的目标(即'InvoiceTaxCodeDetails' ),则将该值push送到适当的数组中
  5. Once the forEach function has completed, then use the native fs module to write the contents of each array to a JSON file一旦forEach函数完成, then使用原生fs模块将每个数组的内容写入 JSON 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM