Rick Rick - 4 months ago 79x
Javascript Question

How to unescape UTF-8 characters in Node (\u00f6)?

I have a properties file which is encoded using ISO Latin but with special characters as UTF-8 escape sequences, for example the following string:

Einstellungen l\u00f6schen

I've tried a bunch of different combinations of
yet none of them do what I need which is to convert these strings to a proper UTF8 format which works with JavaScript. No matter how I go about it the strings always have their UTF8 escape sequences when I print them.

Note that the file is a longer file with some line breaks etc if that makes any difference.

How do I read this file in a way which prints the correct characters?


You either have to parse it as a string-literal, so the unicode-codes are parsed by the engine, therefore you have to wrap it in quotes before running it through JSON.parse().

JSON.parse('"' + str + '"');
//if you use " in your string, you would have to escape it
JSON.parse('"' + str.split('"').join('\\"') + '"');

or you search for the unicode-codes and replace them on your own

str.replace(/\\u([0-9a-fA-F]{4})/g, (m,cc)=>String.fromCharCode("0x"+cc));