I will have a possibly very large JSON file and I want to stream from it instead of load it all into memory. Based on the following statement (I added the emphasis) from JSON::XS
, I believe it won't suit my needs. Is there a Perl 5 JSON module that will stream the results from the disk?
In some cases, there is the need for incremental parsing of JSON texts. While this module always has to keep both JSON text and resulting Perl data structure in memory at one time, it does allow you to parse a JSON stream incrementally. It does so by accumulating text until it has a full JSON object, which it then can decode. This process is similar to using decode_prefix to see if a full JSON object is available, but is much more efficient (and can be implemented with a minimum of method calls).
To clarify, the JSON will contain an array of objects. I want to read one object at a time from the file.
Have you looked at JSON::Streaming::Reader which shows up as first while searching for 'JSON Stream' on search.cpan.org?
Alternatively JSON::SL found by searching for 'JSON SAX' - not quite as obvious search terms, but what you describe sounds like a SAX parsers for XML.
If you have control over how you're generating your JSON, then I suggest turning pretty formatting off and printing one object per line. This makes parsing simple, like so:
In terms of ease of use and speed,
JSON::SL
seems to be the winner:JSON::Streaming::Reader
was okay, but it is slower and suffers from too verbose an interface (all of these coderefs are required even though many do nothing):To parse 1,000 records it took
JSON::SL
.2 seconds andJSON::Streaming::Reader
3.6 seconds (note,JSON::SL
was being fed 4k at a time, I had no control over JSON::Streaming::Reader's buffer size).Did you try to skip first right braket
[
and then the commas,
:like in the third example : http://search.cpan.org/dist/JSON-XS/XS.pm#EXAMPLES
This is what screws your over. A JSON document is one object.
You need to define more clearly what you want from incremental parsing. Are you looking for one element of a large mapping? What are you trying to do with the information you read out/write?
I don't know any library that will incrementally parse JSON data by reading one element out of an array at once. However this is quite simple to implement yourself using a finite state automaton (basically your file has the format
\s*\[\s*([^,]+,)*([^,]+)?\s*\]\s*
except that you need to parse commas in strings correctly.)