0
var http = require('http');
var jsonfile = require('jsonfile');
var util = require('util');

http.createServer(function (req, resp) {

    if (req.method === "GET") {
        var file = "./files/file1.json";
        jsonfile.readFile(file, function (err, obj) {
            if (err) {
                console.log(err);
            }
            console.dir(obj);
        });
    }
}).listen(9000);

HERE IS MY FILE:

{
  "Person": {
    "firstName": "Fawad",
    "lastName": "Surosh"
  }
}

I spent the whole day but couldn't figure it out. It throws an error stating:

SyntaxError: ./files/file1.json: unexpected token ?

gnerkus
  • 11,357
  • 6
  • 47
  • 71
M. Fawad Surosh
  • 450
  • 7
  • 20
  • 2
    you have unicode characters in your json file. copy paste your json data `jsonlint.com` and from there paste json in your file. – Deendayal Garg Mar 28 '16 at 21:55
  • 1
    Your solution partly worked for me. I recreated the file in np++ and it succeeded. – M. Fawad Surosh Mar 28 '16 at 22:09
  • 2
    Tip: IMHO you don't really need to use a module like `jsonfile`. All you need is `fs.readFile()` and `JSON.parse()` in a try-catch block. – mscdex Mar 28 '16 at 22:26

0 Answers0