0

I have a script that uses JSON.parse(datastring); to parse a stringified JSON.

datastrings look like this:

{"_id":"8b8fdd243f734b27829c92e4099f70ec.d","date":1439418654920,"player":"player1","action":"capture"}
{"_id":"a3b7d70d8a074f9ba8b13368ee947f1e.d","date":1439418074476,"player":"player1","action":"capture"}

First one works just fine, but with the second one I get a weird error, that I can't find a way to solve.

undefined:2
{"_id":"a3b7d70d8a074f9ba8b13368ee947f1e.d","date": 1439418074476,"player":"pla
^
SyntaxError: Unexpected token {
    at Object.parse (native)
    at Socket.<anonymous> (/home/ubuntu/workspace/lib/engine.js:12:18)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:427:10)
    at emitReadable (_stream_readable.js:423:5)
    at readableAddChunk (_stream_readable.js:166:9)
    at Socket.Readable.push (_stream_readable.js:128:10)
    at Pipe.onread (net.js:529:21)

Any ideas?

7
  • that's not a valid JSON string Commented Aug 12, 2015 at 23:04
  • @JaromandaX it's generated in the same way as "valid" one before Commented Aug 12, 2015 at 23:05
  • 1
    oh, sorry, I misread the question, I thought that the two lines was the whole string that gave you errors Commented Aug 12, 2015 at 23:06
  • 2
    JSON parsers usually expect only a single, complete value. You currently have 2 independent Objects side-by-side with nothing truly connecting them. Best guess is that you need to wrap them in an Array, separating them with a comma. Commented Aug 12, 2015 at 23:06
  • If you are parsing them together it will not work as it is not valid json. This should work for you though. [{"_id":"8b8fdd243f734b27829c92e4099f70ec.d","date":1439418654920,"player":"player1","action":"capture"}, {"_id":"a3b7d70d8a074f9ba8b13368ee947f1e.d","date":1439418074476,"player":"player1","action":"capture"}] Commented Aug 12, 2015 at 23:07

1 Answer 1

4

JSON.parse expects to be passed a string containing a JSON text, not a string containing multiple JSON texts.

Either parse each line (assuming you can trust that line breaks will only be between the JSON texts in your input) separately…

var json_texts = datastring.split("\n");

… or express the data in an array to start with.

[
    {"_id":"8b8fdd243f734b27829c92e4099f70ec.d","date":1439418654920,"player":"player1","action":"capture"},
    {"_id":"a3b7d70d8a074f9ba8b13368ee947f1e.d","date":1439418074476,"player":"player1","action":"capture"}
]
Sign up to request clarification or add additional context in comments.

7 Comments

datastrings look like this: (note the plural) - First one works just fine, but with the second one I get a weird error - I think the two lines are parsed individually (perhaps retrieved separately?)
@JaromandaX — The error message disagrees. There is no way it would complain about the { that is the first character of the string being an unexpected token.
I started posting exactly the same solution, so I agree with you, but I'm going by his response to my comment it's generated in the same way as "valid" one before and re-reading the question with that in mind
Parse is executed on event where datastring is passed. It's the only string processed at a time.
@fivepointseven That's how node streams work, they are just streams of bytes, they are not grouped by messages or packets or anything. If you want them to do, you'll need to do that explicitly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.