Friday 23 November 2018

NodeJS - AutoConvert a Folder with CSV files to an output folder for JSON files

I am trying to convert a folder with CSV files to an output folder for JSON files.Basically I am trying to convert CSV to JSON and am stuck with some errors.I have loaded 'fs" and 'path' via npm installer in the command line mode.I found the following NodeJS syntax from stackoverflow and it has the followingerror when I tried to run from the command line: fs.js:675 return binding.read(fd, buffer, offset, length, position);See Code Below:// Node packages for file systemvar fs = require('fs');var path = require('path');var filePath = path.join(__dirname, '/CSV_FOLDER');// Read CSVvar f = fs.readFileSync(filePath, {encoding: 'utf-8'},function(err){console.log(err);});// Split on rowf = f.split("\n");// Get first row for column headersheaders = f.shift().split(",");var json = [];f.forEach(function(d){// Loop through each rowtmp = {}row = d.split(",")for(var i = 0; i < headers.length; i++){tmp[headers[i]] = row[i];}// Add object to listjson.push(tmp);});var outPath = path.join(__dirname, '/JSON_FOLDER');// Convert object to string, write json to filefs.writeFileSync(outPath, JSON.stringify(json), 'utf8',function(err){console.log(err);});​

Submitted November 23, 2018 at 02:44PM by AutoGenModerator

No comments:

Post a Comment