3

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?

1 Answer 1

5

I think asynchronous pipe might have something to do with it. You can ensure you only open encoded.csv after it is completed by putting logic in the end event.

var data = fs.createReadStream("myfile.csv");
data.pipe(iconv.decodeStream('utf16'))
  .pipe(iconv.encodeStream('utf8'))
  .pipe(fs.createWriteStream("encoded.csv"));

data.on('end', function() {

  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)
  });
}

https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.