V
V
Valeri Lavrov2017-06-14 16:49:07
JSON
Valeri Lavrov, 2017-06-14 16:49:07

How to write json to file while keeping unicode characters on node js?

Wrote on LAN (Windows 8.1) saves the file with all characters. After the transfer to the hosting (Centos 7) I began to save with "crakozyabry"
Code:

function updatePrice(body, startTimer){
var items = JSON.parse(fs.readFileSync('items.json', 'utf8'));
for (index = 0; index < body.items.length; ++index) {
    var price = body.items[index].price;
    var name = body.items[index].name;
    var idItem = find(items, name);
    if(~idItem){
    items[idItem].jar = price;
    items[idItem].ujar = new Date();
    }
}
  var items = JSON.stringify(items,"",4);
  fs.writeFileSync('prices.json', items);
  var elapsed = new Date().getTime() - startTimer;
  logger.trace("Обновлено "+index+" предметов");
  logger.trace("Время выполнения: "+elapsed+" мс.\n");
}

Name in items.json
StatTrak\u2122 AK-47 | Aquamarine Revenge (Battle-Scarred)

Output name in prices.json
StatTrakâ„¢ AK-47 | Aquamarine Revenge (Factory New)

Tried different encodings. Google suggests simply replacing these characters. I guess it's because of the operating system.
Node version on local 6.9.1, on hosting 6.11.0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2017-06-14
@Ablovka

Specify encoding explicitly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question