Incremental JSON parsing in php Incremental JSON parsing in php json json

Incremental JSON parsing in php


Update

I've written a small class that does char-by-char JSON input parsing..https://github.com/janeklb/JSONCharInputReader

Fresh off the presses so it's probably got a few bugs.. if you decide to try it out, let me know!

--

Could you (while keeping track of '{', '[', ']', '}' scope) break the stream up on each comma that's not part of a string value?And then process each token using json_decode()?

This solution would work best if the json stream didn't have a lot of large objects (as they would only be parsed once they've arrived in full).

Edit: if it does have large objects, this strategy could be modified to look a little 'deeper' instead.. but the complexity here would shoot up.


I've written a SAX-like JSON streaming parser that should do the trick. Let me know if it works!


There's a simple work-around, if each individual element is guaranteed to be received in it's entirety, or in other words - you can't get e.g. just the half of an object like this:

{"a": 1,

json_decode() will return NULL because the string you're passing to it is not a valid JSON string. Replace the trailing comma with an ending bracket and there you go:

[1, 2, {"id": 3}]

There's no problem in decoding it now and wait for other parts of the stream to be received later.