Convert utf-8 codes to unicode


Well, I have a JSON where all unicode symbols like this "★" are in this format: "\ u2605" have some way to convert those codes to the symbols when my program NodeJS read the JSON ?

Example of how it is: {"name":"\u2605 Bayonet","price":15713,"have":6,"max":6}

Example of how I want it to stay: {"name":"★ Bayonet","price":15713,"have":6,"max":6}

I've even manually replaced replace with these codes, but when I run it, it repeats JSON twice, giving replace only the first time and does not stores the changed JSON variable.

My code:

Trade.prototype.getSteamapis = function getSteamapis(callback) {
  fs.readFile('./prices/730.json','utf8',function (err,body) {
    if (err) {
      return console.log(err);
    body.replace("/\u2605 /g","★")
    return body
asked by anonymous 24.08.2017 / 16:47

2 answers


First, these \uXXXXX characters are not UTF-8, they are "escaped" characters (actually any character can be escaped like this).

Second, this is what you want is not to convert UTF-8 to Unicode, it's just to do the inverse of "escape", it would be an "unescape".

To understand the "supposed differences" between UTF-8 and Unicode I recommend that you read this:

Then is not necessary to convert anything, this is a kind of "escape" to avoid "truncating the data", just do the test, the moment you pop the JSON into an HTML element it will be reproduced as it should, as long as your page uses charset=UTF-8 .

So assuming your page has the header in the server response:

 Content-Type: text/html; charset=UTF-8

And / Or the tag (if it is a browser with support for HTML5):

 <meta charset="utf-8">

Browsers a little older:

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Just popular Json (assuming the response came from anywhere, for example via Ajax or was already within the <script> tag):

var meujson = {"name":"\u2605 Bayonet","price":15713,"have":6,"max":6};

document.getElementById("foo-name").textContent =;
<div id="foo-name"></div>

These escapes are necessary, as I said earlier, to avoid loss of characters in different encodings.

24.08.2017 / 17:46

var s = "{\"name\":\"\u2605 Bayonet\",\"price\":15713,\"have\":6,\"max\":6}";
var s2 = JSON.parse('"' + s.replace(/\"/g, '\"') + '"');

I based this answer here on SOen and also this one here .

The idea is that JSON.parse is able to make the necessary conversions. But since you want to convert a string (regardless of whether it is a JSON) and output a string, '"' + s.replace(/\"/g, '\"') + '"' adds the double quotation marks at the beginning and escapes the double quotation marks that occur in the text, ensuring that the string result will be a string.

24.08.2017 / 16:56