I have written the below code for converting json to csv.
var json2csv = require('json2csv');
var fs = require('fs');
var fields = ['car.make', 'car.model', 'price', 'color'];
var myCars = [
{
"car": {"make": "萱萱的 iPad Mini", "model": "萱萱的 iPad Mini"},
"price": 40000,
"color": "blue"
}, {
"car": {"make": "萱萱的 iPad Mini", "model": "萱萱的 iPad Mini"},
"price": 35000,
"color": "black"
}, {
"car": {"make": "萱萱的 iPad Mini", "model": "萱萱的 iPad Mini"},
"price": 60000,
"color": "green"
}
];
var csv = json2csv({ data: myCars, fields: fields });
fs.writeFile('file.csv', csv, { encoding: 'utf8' }, function(err) {
if (err) throw err;
console.log('file saved');
});
the above code, will create csv file, but unable to show chinese characters as expected in the columns. because csv is not saved as utf8 format.
I have created a sample JSON object for explaining how chinese will be embed in values of json object.
What is the expected parameter to pass for saving all kind of utf language encodings for the above mentioned code.