Created
January 29, 2025 10:36
-
-
Save tkoz0/a58dd3bdc4832c57ddf7e5041d3ec5c8 to your computer and use it in GitHub Desktop.
reading multiple json objects from a string
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 | |
def multi_json(s:str): | |
d = json.JSONDecoder() | |
i = 0 | |
while i < len(s): | |
if s[i] in ' \t\n': | |
i += 1 | |
else: | |
obj,i = d.raw_decode(s,i) | |
yield obj | |
s1 = '{}{}"abc"12""3 1.0truefalsenullnull' | |
print(list(multi_json(s1))) | |
s2 = ' 12 -1.8 "string" null [1,2, "" ] 0' | |
print(list(multi_json(s2))) | |
s3 = '[][1][[]]"word"-5true {}' | |
print(list(multi_json(s3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment