[英]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 对象-
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 文件”,尝试一些类似的东西:
forEach
function to iterate through InvoiceRecords
forEach
函数遍历InvoiceRecords
for...of
statement to iterate through the Object.entries
for...of
语句遍历Object.entries
'InvoiceTaxCodeDetails'
), push
the value into the appropriate array'InvoiceTaxCodeDetails'
),则将该值push
送到适当的数组中forEach
function has completed, then
use the native fs
module to write the contents of each array to a JSON fileforEach
函数完成, then
使用原生fs
模块将每个数组的内容写入 JSON 文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.