I am reading data from a simple csv file and adding some data from a web service into an array using Node JS fs. The array is defined as:
var allDataOut=[];
The addition of the data happens at this point in the code:
var cumulate = {
"Details":augment,
"Timestamp": new Date(),
data:data
}
allDataOut.push(cumulate);
console.log(allDataOut);
callback(cumulate);
According to the console.log I am seeing exactly what I would expect with the correct data sitting inside the [] denoting an array.
So to create a JSON I would expect the JSON.stringify(allDataOut) would give me the correct result.
console.log("writing file");
var str = JSON.stringify(allDataOut);
console.log("This is output .stringify" + str);
fs.appendFileSync(outputFile, str, encoding='utf8');
but as you can see in the final data below it is still in the array format. I have seen a number of questions on this subject in this forum with one suggestion of using JSON.parse(JSON.stringify(allDataOut)); - unfortunately nothing seems to change the output.
[ {
"Details": {
"Name": "Tophat",
"SegmentID": "0",
"DistanceToDepot": "256.53"
},
"Timestamp": "2016-04-10T11:40:03.291Z",
"data": {
"metadata": {
"language": "en-US",
"transaction_id": "1460288403143:279662660",
"version": "1",
"Altitude": 49.44
},
"observation": {
"class": "observation",
"expire_time_gmt": 1460289003,
"metric": {
"wspd": 21,
"gust": null,
"vis": 9.7
}
}
} }, {
"Details": {
"Name": "Tophat",
"SegmentID": "25659",
"DistanceToDepot": "0"
},
"Timestamp": "2016-04-10T11:40:40.428Z",
"data": {
"metadata": {
"language": "en-US",
"transaction_id": "1460288440297:-647605523",
"version": "1",
"Altitude": 50.68
},
"observation": {
"class": "observation",
"expire_time_gmt": 1460288452,
"metric": {
"wspd": 18,
"gust": null,
"vis": 12.91
}
}
} } ]