Reputation: 929
I'm using NodeJS to read a series of keys and values from a spreadsheet to build a JSON object, and then writing that object to a file, e.g. | key | value | becomes the object {"key": "value"}
I'm using JSON.stringify to convert the objects to strings, and then writing this string to a file
fs.writeFileSync('object.json', JSON.stringify(object, null, 2));
The spreadsheet contains some unicode characters, which need to be stored as their encoded value, e.g. é as \u00E9, which is not handled by the snippet above.
How can I encode unicode characters in the file so, for example, "Numéro" is stored as "Num\u00E9ro"
Upvotes: 0
Views: 3615
Reputation: 4678
You can use a plugin to encode your char string, or you can do it by hand with a simple regex :
exports.unicodeAccent = function(str)
{
var charset = [
"€",
"À",
"Á",
"Â",
"Ã",
"Ä",
"Å",
"à",
"á",
"â",
"ã",
"ä",
"å",
"Ò",
"Ó",
"Ô",
"Õ",
"Õ",
"Ö",
"Ø",
"ò",
"ó",
"ô",
"õ",
"ö",
"ø",
"È",
"É",
"Ê",
"Ë",
"è",
"é",
"ê",
"ë",
"ð",
"Ç",
"ç",
"Ð",
"Ì",
"Í",
"Î",
"Ï",
"ì",
"í",
"î",
"ï",
"Ù",
"Ú",
"Û",
"Ü",
"ù",
"ú",
"û",
"ü",
"Ñ",
"ñ",
"Š",
"š",
"Ÿ",
"ÿ",
"ý",
"Ž",
"ž"
];
for(var i = 0; i < charset.length; i++)
{
var code = ("00000000"+(charset[i].charCodeAt(0).toString(16))).slice(-4);
var unicodeStr = "\\u"+code;
var re = new RegExp(charset[i], "gm");
str = str.replace(re, unicodeStr);
}
return str;
}
Upvotes: 1