Skip to content

Instantly share code, notes, and snippets.

@maxfischer2781
Created March 30, 2017 10:24
Show Gist options
  • Save maxfischer2781/5e15e7d627ecd374a028593cb6c7376c to your computer and use it in GitHub Desktop.
Save maxfischer2781/5e15e7d627ecd374a028593cb6c7376c to your computer and use it in GitHub Desktop.
Parse multiple jsons from one input blob
import json
test_json = """\
[
1,
2,
3
]
[
4,
5,
6
]"""
def split_schedds(schedds_iter):
exhausted = [False]
def schedd_iter():
"""Iterator for individual schedd block"""
for line in schedds_iter:
yield line
if line == ']\n':
break
else:
exhausted[0] = True
while not exhausted[0]:
_buffer = list(schedd_iter())
if _buffer:
yield _buffer
def parse_schedd(schedd_iter):
buff = ''.join(schedd_iter)
return json.loads(buff)
for schedd in split_schedds(iter(line + '\n' for line in test_json.split('\n'))):
print(parse_schedd(schedd))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment