Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I parse the first JSON object on a stream in JS
    primarykey
    data
    text
    <p>I have a stream of JSON objects, as with JSON-RPC over TCP or WebSockets. There's no length prefix or delimiter, because JSON is self-delimiting. So, when I read from the stream, I may end up with something like this:</p> <pre><code>{"id":1,"result":{"answer":23},"error":null} {"id":2,"result":{"answer":42},"error":null} {"id":3,"result":{"answ </code></pre> <p>I need to parse each JSON object one by one. I can't do this with JSON.parse, because it will just throw a syntax error for extraneous data at the end.</p> <p>Of course with that example I could go line by line, but I can't rely on the whitespace looking like that; JSON-RPC can just as easily look like this:</p> <pre><code>{ "id": 1, "result": { "answer": 23 }, "error":null } </code></pre> <p>Or this:</p> <pre><code>{"id":1,"result":{"answer":23},"error":null}{"id":2,"result":{"answer":42},"error":null} </code></pre> <p>With most parsers in other languages, the obvious answer is something like this (using Python as an example):</p> <pre><code>buf = '' decoder = json.JSONDecoder() def onReadReady(sock): buf += sock.read() obj, index = decoder.raw_decode(buf) buf = buf[index:] if obj: dispatch(obj) </code></pre> <p>But I can't find anything similar in JS. I've looked at every JS parser I can find, and they're all effectively equivalent to JSON.parse.</p> <p>I tried looking at various JSON-RPC frameworks to see how they handle this problem, and they just don't. Many of them assume that a recv will always return exactly one send (which works fine for JSON-RPC over HTTP, but not over TCP or WebSockets—although it may appear to work in local tests, of course). Others don't actually handle JSON-RPC because they add requirements on whitespace (some of which aren't even valid for JSON-RPC).</p> <p>I could write a delimiter check that balances brackets and quotes (handling escaping and quoting, of course), or just write a JSON parser from scratch (or port one from another language, or modify <a href="http://code.google.com/p/json-sans-eval/" rel="nofollow noreferrer">http://code.google.com/p/json-sans-eval/</a>), but I can't believe no one has done this before.</p> <p>EDIT: I've made two versions myself, <a href="http://pastebin.com/fqjKYiLw" rel="nofollow noreferrer">http://pastebin.com/fqjKYiLw</a> based on json-sans-eval, and <a href="http://pastebin.com/8H4QT82b" rel="nofollow noreferrer">http://pastebin.com/8H4QT82b</a> based on Crockford's reference recursive descent parser json_parse.js. I would still prefer to use something that's been tested and used by other people rather than coding it myself, so I'm leaving this question open.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload