title: Node.js SyntaxError: Unexpected token in JSON at position 0
body: <p>I have a JavaScript application which is running in Node.js environment and communicates to its clients ( also in JavaScript ) using a ZeroMQ. The messages come on the server in JSON format. </p>
  
  <p>The application code throws it out</p>
  
  <blockquote>
    <p><code>Node.js SyntaxError: Unexpected token  in JSON at position 0</code></p>
  </blockquote>
  
  <p>when it is parsed using JSON.parse(). I'm unable to figure out the issue. I've verified the JSON using <a href="http://jsonlint.com" rel="nofollow noreferrer">http://jsonlint.com</a></p>
  
  <p>Any help with JSON.parse() is welcome.</p>
  
  <p>Edited:01/10/17, 15:33
  Here are the client and server JavaScript code files. You'll need to create the .js files, can't post such a big code.
  The JSON data file is also provided.
  You'll need to launch the server.js and client.js and then the server console will print out the exception for unrecognized character.</p>
  
  <p><a href="https://www.4shared.com/folder/6VFJqrgU/javascript.html" rel="nofollow noreferrer">https://www.4shared.com/folder/6VFJqrgU/javascript.html</a></p>
  
  <p>Stackoverflow imposes link posting restrictions so had to post one link with all the files.</p>
  
  <p>Just for info, I'm a C++ programmer, so don't bother about the code formatting or style of programming. I had to do it for a project need.</p>
  
  <p>Edit 02/10/17, 11:50: Well it turns out that it is the JSON.parse() method which is unable to parse the json. But, I added a .trim() call to the args[1].toString() and the error has moved downstream. Unexpected token o in JSON at position 10. I don't understand what is wrong!!</p>
  
  <p>Edit 04/10/17: Here is the minimal code.</p>
  
  <pre><code>var fs = require('fs');
  
  try
  {         
    var event = fs.readFileSync('demoReport.json', 'utf8');
    console.log(event);
    var eventObj = JSON.parse(event);
  
    var reportName = event["ReportName"];
    var reportData = event["ReportData"];
  
    console.log(reportData);
  }
  catch(error)
  {
      console.log("JSON parsing failed: " + error);         
  }
  </code></pre>
  
  <p>This is the json:</p>
  
  <pre><code>{"EventName":"ReportGenEvent","TemplateFileNameLocation":"File location","ReportFormat":".pdf","ReportName":"TestReport","ReportLocation":"report location","Locale":"French","ReportData":{"dateTime":"2017-09-29T00:05:22.824Z","streamName":"","measurementTime":"2017-04-01T01:13:25.000Z","durationSeconds":0.0,"outOfBand":false,"notFinal":false,"newMeasurement":false,"savedFileName":"","measurementType":"Unknown","analysisElapsedSeconds":1.3462,"analysisElapsedCPUSecs":0.0624004,"geometryID":"GEOM","geometryDescription":"","measurementUUID":"6060c80f-007c-4992-88f8-55e2200d99b7","backgroundUUID":"","measurementWorkflowID":"Measurement","instrumentProperties":{"classCode":8,"description":"","manufacturer":"","model":"","properties":"locationName=Home latitude=25 longitude=20 elevation=30","serialNumber":"product/1","versionInformation":"=V1.0"}}}
  </code></pre>
  
  <p>Thanks.</p>