Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:45:09+00:00 2026-06-02T18:45:09+00:00

I have a stream of JSON objects, as with JSON-RPC over TCP or WebSockets.

  • 0

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:

{"id":1,"result":{"answer":23},"error":null}
{"id":2,"result":{"answer":42},"error":null}
{"id":3,"result":{"answ

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.

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:

{
  "id": 1, 
  "result": {
    "answer": 23
  },
  "error":null
} 

Or this:

{"id":1,"result":{"answer":23},"error":null}{"id":2,"result":{"answer":42},"error":null}

With most parsers in other languages, the obvious answer is something like this (using Python as an example):

buf = ''
decoder = json.JSONDecoder()
def onReadReady(sock):
  buf += sock.read()
  obj, index = decoder.raw_decode(buf)
  buf = buf[index:]
  if obj:
    dispatch(obj)

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.

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).

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 http://code.google.com/p/json-sans-eval/), but I can’t believe no one has done this before.

EDIT: I’ve made two versions myself, http://pastebin.com/fqjKYiLw based on json-sans-eval, and http://pastebin.com/8H4QT82b 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:45:11+00:00Added an answer on June 2, 2026 at 6:45 pm

    After a month of searching for alternatives and not finding anything useful, I decided to code up a bunch of different implementations and test them out, and I went with my modification of Crockford’s reference recursive-descent parser (as described in the question, available here).

    It wasn’t the fastest, but it was more than fast enough in every test I did. More importantly, it catches clearly erroneous JSON, when that’s not ambiguous with incomplete JSON, much better than most of the other alternatives. Most importantly, it required very few, and pretty simple, changes from a well-known and -tested codebase, which makes me more confident in its correctness.

    Still, if anyone knows of a better library than mine (and just being used by lots of projects instead of just me would count as a major qualification), I’d love to know about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to subscribe from GWT to JSON objects stream and listen
I have a JSON stream being generated by a server side C++ program that
I have a stream of data coming in over the serial line from an
I have setup a basic app which pulls down a JSON stream and displays
I have a json file/stream, i like to be able to make select SQL
I have response stream from a ftp web request that returns binary file. I
I have a stream of data that I want to place into a container.
I have a stream of bytes which contains a flag which identifies the endianness
I often run into the problem that I have one stream full of data
I have a input stream IPCimstream, which returns a pointer to the character buffer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.