Skip to content

Instantly share code, notes, and snippets.

@hwine
Created September 8, 2017 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwine/535373d0148b9dd83c0f484619787e58 to your computer and use it in GitHub Desktop.
Save hwine/535373d0148b9dd83c0f484619787e58 to your computer and use it in GitHub Desktop.
# pip install jsonstreams
import jsonstreams
# example from docs
mylist = list(range(10))
mydict = {a: b for a in range(10) for b in 'abcdefghij'}
# NB - arguement order changed from doc example.
# see https://github.com/dcbaker/jsonstreams/issues/13
with jsonstreams.Stream(jsonstreams.Type.object, 'foo') as s:
s.write('list', mylist)
s.write('dict', mydict)
# Showing for array, and using indent
# sort_keys not supported
fname = '/tmp/jstream.json'
s = jsonstreams.Stream(jsonstreams.Type.array, fname, indent=4)
d1 = {k: k**3 for k in range(5)}
d2 = {k: str(k**3) for k in range(15, 10, -1)}
s.write(d1)
s.write(d2)
# must explicitly close -- that's what outputs the closing delimiter
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment