I'm using BabyParse to convert a local CSV file to JSON. Here's the js file I've written that does it:
var Baby = require('babyparse');
var fs = require('fs');
var file = 'test2.csv';
var content = fs.readFileSync(file, { encoding: 'binary' });
parsed = Baby.parse(content, {fastMode: false});
rows = parsed.data;
console.log(rows);
fs.writeFile("blahblahblah.json", rows, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
The JSON outputted to the console by the console.log(rows) line seems to be correct(ish). However, when I write rows to a file, all the JSON boilerplate disappears. For example, here's what I get when trying to convert the following csv file:
col1,col2,col3
"val1","val2","val3"
"val1","val2","val3"
"val1","val2","val3"
This is what gets printed to console:
[ [ 'col1', 'col2', 'col3' ],
[ 'val1', 'val2', 'val3' ],
[ 'val1', 'val2', 'val3' ],
[ 'val1', 'val2', 'val3' ],
[ '' ] ]
But this is what gets written to the file:
col1,col2,col3,val1,val2,val3,val1,val2,val3,val1,val2,val3,
Does anyone know what's happening here? Why is there a difference?
via Adam
No comments:
Post a Comment