Created
November 8, 2025 14:30
-
-
Save nitori/18a65743f9d0219e7f94ae4ef87c3faf to your computer and use it in GitHub Desktop.
continuous non-line delimited json stream
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 subprocess, json | |
| def parse_json_dynamic(stream): | |
| dec = json.JSONDecoder() | |
| buf = '' | |
| for chunk in stream: | |
| buf += chunk | |
| while True: | |
| try: | |
| obj, idx = dec.raw_decode(buf) | |
| except json.JSONDecodeError: | |
| break | |
| yield obj | |
| buf = buf[idx:].lstrip() | |
| import io | |
| f = io.StringIO() | |
| f.write("{\n") | |
| f.write(' "hello": "world",\n') | |
| f.write(' "nice": "test"\n') | |
| f.write("}\n") | |
| f.write("{\n") | |
| f.write(' "hello": "world",\n') | |
| f.write(' "nice": "test"\n') | |
| f.write("}\n") | |
| f.seek(0) | |
| for obj in parse_json_dynamic(f): | |
| print(obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment