Screen_Shot_2014-05-30_at_1.37.33_PM

Salsify Engineering Blog

JSON Streaming Parser for PHP

Posted by Rob Gonzalez

Mar 20, 2013 4:54:00 PM

json-streaming-parser-for-phpRecently I looked around for a JSON streaming parser written in PHP and couldn't find one. So I wrote my own and am making it available to anyone who wants to use it.

For those unfamiliar with streaming parsers I'll give a brief intro. In short, they're useful if you have a very large document and don't want to have to load the whole thing in memory at once.

Most (non-streaming) parsers—including, for example, the stock JSON parsers that come with PHP—will load an entire document in memory and then give you access to the whole thing at once. The advantage to this whole-document-at-once approach is that you get random access to every object in the document. Also the programmatic interface they provide can be really convenient. In PHP's standard library you can get a handle on a native PHP array for the entire JSON document, which is really easy to work with.

The downside is, as mentioned, the whole document must be loaded into memory. For most web servers or web services this is a serious constraint. Furthermore, this means that your application can't start actually doing anything useful with the data until the whole thing is loaded in memory. A streaming parser, in contrast, gives your application access to data almost as soon as the data is read by PHP so can be much faster.

As I was writing a plugin for Magento, I needed a JSON streaming parser written in PHP. I looked at StackOverflow, PHP.net, and finally Google. It's pretty crazy to me that no one seems to have had this need before given the popularity of both JSON and PHP, but there you go!

JSON Streaming Parser Example

Here's an example of using the JSON streaming parser in action:

The key element here is the YourListener class. Streaming parsers use an event model which requires a listener. Basically as soon as an "event" happens (meaning some object or value is parsed from the JSON document) the listener is notified. The interface is pretty straightforward. Here's an example of a complete listener implementation:

You can find the code at GitHub. Hopefully this will save you some time.

comments powered by Disqus

Recent Posts