Skip to content

Instantly share code, notes, and snippets.

@tsuchm
Last active January 8, 2026 04:36
Show Gist options
  • Select an option

  • Save tsuchm/bcb79db7fe9750eb34e3dbb872718fae to your computer and use it in GitHub Desktop.

Select an option

Save tsuchm/bcb79db7fe9750eb34e3dbb872718fae to your computer and use it in GitHub Desktop.
import json
from itertools import pairwise
def sentinel(iterable):
for x in iterable:
yield x
yield None
def write_json_streaming_style(generator, indent=4):
print("[")
for cur,post in pairwise(sentinel(generator)):
text = json.dumps(cur, ensure_ascii=False, indent=indent)
if post:
text += ","
for line in text.split("\n"):
print(" " * indent + line)
print("]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment