I am extracting some compressed JSON over TCP in node.js to parse them. So my approach is similar to this. I shorten and simplify it, so you do not need to know the surrounding logic.
socket.on("data", function(data) { console.log(data.toString());
Full (decorated) JSON is this. As you can see, there are no errors.
{ "result": "success", "source": "chat", "success": { "message": "test", "time": 1331770513, "player": "silvinci" } }
But JSON.parse(data.toString()) always throws me this dumb error:
{"result":"success","source":"console","success":{"time":1331762264,"line":"20 ^ SyntaxError: Unexpected token { at Object.parse (native) at Socket.<anonymous> (/home/node/api.js:152:35) // irrelevant from here on at Socket.emit (events.js:67:17) at TCP.onread (net.js:347:14)
So I ask: "What could be wrong with JSON-String. Try it directly, should not work." Surprise Surprise! It worked. Why does this work when I directly enter a string?
source share