0

When I read a JSON file in node.js the non-english character display as '?' instead of the origin character.
The results from the stream is already in the wrong charset.

var fs = require('fs');
var path = require('path');

var stream = fs.createReadStream(path.join(__dirname,'../data.json'), 'utf8');

        stream.on('error', function (error) {
            response.status(500).send({ msg: error.message });
        })

        stream.pipe(response);
    }
}

File example:

   {  
       data: [  
          {"id":"111","name":"Dr. Per Änglund","phone":"7350-01794"},  
          {"id":"22","name":"Lars Änglund","phone":"1942-463945"}  
      ]  
   }
Sari
  • 3
  • 4

1 Answers1

1

You need to take into consideration the encoding of the file you're trying to read.

Assuming you read your file using

fs.readFile("test.txt", function(err, data) {
   console.log( data );
});

You can simply add the 'utf8' argument to specify that you're trying to read an UTF-8 file:

fs.readFile("test.txt", 'utf8', function(err, data) {
   console.log( data );
});

See also this question.

Community
  • 1
  • 1