If you're trying to avoid loading the whole list into memory, you could process the file as text first:
Use a stack to keep track of open and closing of brackets/quotes. Scan through the string for any of the openers, or the current closer. When scanning through text, look only for the text closer. Push one on when you read the opener, and pop it off when you find the closer.
The full set for JSON is [
-> ]
, {
-> }
and "
-> "
. You should exclude \"
though. You can check the spec at http://www.json.org/
Then whenever a ]
is encountered and the stack only has one item (the top level '[') after popping the matching [
, then you know it's time to start a new line.
Finally, you should ensure the first [
and last ]
don't appear in your output.
That will give you separate JSON objects for each item of the list, each on a separate line of the file.
If you dig into the python JSON library, there should be some functions that parse JSON too. You could leverage those, even though they aren't part of the public interface.
Of course, you can achieve the same by loading the string using the JSON library and then dumping it item by item (or multiple items) as per the other answer.