Is there a streaming API for JSON?

JsonStreaming

Json Problem Overview


Is DOM the only way to parse JSON?

Json Solutions


Solution 1 - Json

Some JSON parsers do offer incremental ("streaming") parser; for Java, at least following parsers from json.org page offer such an interface:

(in addition to Software Monkey's parser referred to by another answer)

Actually, it is kind of odd that so many JSON parsers do NOT offer this simple low-level interface -- after all, they already need to implement low-level parsing, so why not expose it.

EDIT (June 2011): Gson too has its own streaming API (with gson 1.6)

Solution 2 - Json

By DOM, I assume you mean that the parser reads an entire document at once before you can work with it. Note that saying DOM tends to imply XML, these days, but IMO that is not really an accurate inference.

So, in answer to your questions - "Yes", there are streaming API's and "No", DOM is not the only way. That said, processing a JSON document as a stream is often problematic in that many objects are not simple field/value pairs, but contain other objects as values, which you need to parse to process, and this tends to end up a recursive thing. But for simple messages you can do useful things with a stream/event based parser.

I have written a pull-event parser for JSON (it was one class, about 700 lines). But most of the others I have seen are document oriented. One of the layers I have built on top of my parser is a document reader, which took about 30 LOC. I have only ever used my parser in practice as a document loader (for the above reason).

I am sure if you search the net you will find pull and push based parsers for JSON.

EDIT: I have posted the parser to my site for download. A working compilable class and a complete example are included.

EDIT2: You'll also want to look at the JSON website.

Solution 3 - Json

As stefanB mentioned, http://lloyd.github.com/yajl/ is a C library for stream parsing JSON. There are also many wrappers mentioned on that page for other languages:

> * yajl-ruby - ruby bindings for YAJL > * yajl-objc - Objective-C bindings for YAJL > * YAJL IO bindings (for the IO language) > * Python bindings come in two flavors, py-yajl OR yajl-py > * yajl-js - node.js bindings (mirrored to github). > * lua-yajl - lua bindings > * ooc-yajl - ooc bindings > * yajl-tcl - tcl bindings

some of them may not allow streaming, but many of them certainly do.

Solution 4 - Json

Disclaimer: I'm suggesting my own project.

I maintain a streaming JSON parser in Javascript which combines some of the features of SAX and DOM:

Oboe.js website

The idea is to allow streaming parsing, but not require the programmer to listen to lots of different events like with raw SAX. I like SAX but it tends to be quite low level for what most people need. You can listen for any interesting node from the JSON stream by registering JSONPath patterns.

The code is on Github here:

Oboe.js Github page

Solution 5 - Json

If you want to use pure javascript and a library that runs both in node.js and in the browser you can try clarinet:

https://github.com/dscape/clarinet

The parser is event-based, and since it’s streaming it makes dealing with huge files possible. The API is very close to sax and the code is forked from sax-js.

Solution 6 - Json

LitJSON supports a streaming-style API. Quoting from the manual:

"An alternative interface to handling JSON data that might be familiar to some developers is through classes that make it possible to read and write data in a stream-like fashion. These classes are JsonReader and JsonWriter.

"These two types are in fact the foundation of this library, and the JsonMapper type is built on top of them, so in a way, the developer can think of the reader and writer classes as the low-level programming interface for LitJSON."

Solution 7 - Json

Here's a NodeJS NPM library for parsing and handling streams of JSON: https://npmjs.org/package/JSONStream

Solution 8 - Json

If you are looking specifically for Python, then ijson claims to support it. However, it is only a parser, so I didn't come across anything for Python that can generate json as a stream.

For C++ there is rapidjson that claims to support both parsing and generation in a streaming manner.

Solution 9 - Json

For Python, an alternative (apparently lighter and more efficient) to ijson is jsaone (see that link for rough benchmarks, showing that jsaone is approximately 3x faster).

DISCLAIMER: I'm the author of jsaone, and the tests I made are very basic... I'll be happy to be proven wrong!

Solution 10 - Json

Answering the question title: YAJL a JSON parser library in C:

> YAJL remembers all state required to > support restarting parsing. This > allows parsing to occur incrementally > as data is read off a disk or network.

So I guess using yajl to parse JSON can be considered as processing stream of data.

Solution 11 - Json

In reply to your 2nd question, no, many languages have JSON parsers. PHP, Java, C, Ruby and many others. Just Google for the language of your choice plus "JSON parser".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkalView Question on Stackoverflow
Solution 1 - JsonStaxManView Answer on Stackoverflow
Solution 2 - JsonLawrence DolView Answer on Stackoverflow
Solution 3 - JsonpyklerView Answer on Stackoverflow
Solution 4 - JsonjimhigsonView Answer on Stackoverflow
Solution 5 - JsondscapeView Answer on Stackoverflow
Solution 6 - JsonAgnel KurianView Answer on Stackoverflow
Solution 7 - JsonTom ChapinView Answer on Stackoverflow
Solution 8 - JsonharidsvView Answer on Stackoverflow
Solution 9 - JsonPietro BattistonView Answer on Stackoverflow
Solution 10 - JsonstefanBView Answer on Stackoverflow
Solution 11 - JsonRyan DohertyView Answer on Stackoverflow