1

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.

The application code throws it out

Node.js SyntaxError: Unexpected token in JSON at position 0

when it is parsed using JSON.parse(). I'm unable to figure out the issue. I've verified the JSON using http://jsonlint.com

Any help with JSON.parse() is welcome.

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.

https://www.4shared.com/folder/6VFJqrgU/javascript.html

Stackoverflow imposes link posting restrictions so had to post one link with all the files.

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.

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!!

Edit 04/10/17: Here is the minimal 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);         
}

This is the json:

{"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"}}}

Thanks.

dstack
  • 31
  • 3
  • And where is the problem's MCVE to reproduce the issue and inspect it's behaviour? – user3666197 Sep 30 '17 at 09:37
  • Let me try to build one because it is slightly complicated to carve it out due to sensitive data. – dstack Oct 01 '17 at 12:15
  • Could you please post code in the question itself (you have a nice "Code Sample" button to make it look nice) and only share the relevant bits? That link of yours makes me feel like hunting for TV series in a torrent directory. (My guess though is that jsonlint.com is right but you aren't feeding it with the exact output generated by your code.) – Álvaro González Oct 02 '17 at 10:00
  • @ÁlvaroGonzález I've uploaded the code. But it is strange that you get redirected to torrents; you should scan your PC with a good antivirus, malware remover and remove adware. – dstack Oct 04 '17 at 09:56
  • I never said that—I only pointed out that the linked site guides you through a series of pages for a simple download and is full of ads with fake download buttons... just like dubious torrent sites do. – Álvaro González Oct 05 '17 at 07:52
  • Is `demoReport.json` saved with BOM? – Álvaro González Oct 05 '17 at 07:53
  • Sorry about the misunderstanding. Yes, it was saved with BOM. It is a file which I received from a colleague and he confirmed it. At the moment, I don't have a way of verifying this. – dstack Oct 06 '17 at 14:32
  • The BOM should explain why it isn't valid JSON, yet jsonlint won't report it because the BOM can easily be left out when copying text though the clipboard. Let me find a previous question and link this to it. – Álvaro González Oct 09 '17 at 15:47
  • Possible duplicate of [node.js readfile error with utf8 encoded file on windows](https://stackoverflow.com/questions/24356713/node-js-readfile-error-with-utf8-encoded-file-on-windows) – Álvaro González Oct 09 '17 at 15:49

0 Answers0