I have a csv file with utf-16 encoding, and need to change its enconding to utf8 and convert it to JSON. I'm using csvtojson and iconv-lite modules. Here is my code:
var data = fs.createReadStream("myfile.csv");
data.pipe(iconv.decodeStream('utf16'))
.pipe(iconv.encodeStream('utf8'))
.pipe(fs.createWriteStream("encoded.csv"));
var Converter = require("csvtojson").Converter;
var csvStr = fs.readFileSync("encoded.csv").toString();
var converter = new Converter({});
converter.fromString(csvStr, function(err, jsonObj) {
if (err) {
handleError(err)
}
console.log(jsonObj)
});
The problem is that iconv converts the csv file with the right encoding, but when I read this file and call toString() method, it returns an empty string. How can I fix this?