import json def jsonstream(fd): while True: try: here = fd.tell() j = json.load(fd) # if json.load returned successfully, there was only one json doc # in the file, so yield it and return from the generator. # When there is more than 1 doc, json.load throws an exception, # and the exception handler further down extracts the # first one by examining the exception data and re-parsing. # Of course this is awful but it suffices for now. yield j return except json.decoder.JSONDecodeError as e: hlen = e.pos fd.seek(here) jh = fd.read(hlen) assert len(jh)==hlen j = json.loads(jh) yield j