1

I have a JSON file with discrete objects that are not enclosed in another composite object (e.g. an array). The reason for this is that I don't want to rely on the assumption that they will all fit into memory.

In other words, I'd like to iterate over them, reading, parsing, and processing them one at a time.

In YAML I can do this using YAML.load_documents. Is there such a thing in (Ruby) JSON?

Thanks, Keith

1
  • I think I understand. An example in the question would be useful though. Commented Jul 3, 2013 at 16:58

1 Answer 1

1

So basically you want a streaming parser for JSON.

Try yajl-ruby, a gem with ruby bindings to the Yajl JSON streaming parser library, or JSON::Stream a streaming pure-ruby JSON parser.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I used yajl-ruby. I couldn't find an example of handling parsed records one at a time, but came up with this method, which works for me: gist.github.com/keithrbennett/5930475.
Don't forget the Oj gem. It provides a significantly faster streaming JSON parser interface via Oj::ScHandler (simple callback handler).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.