Created
March 30, 2017 10:24
-
-
Save maxfischer2781/5e15e7d627ecd374a028593cb6c7376c to your computer and use it in GitHub Desktop.
Parse multiple jsons from one input blob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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