Skip to content

Instantly share code, notes, and snippets.

@tkoz0
Created January 29, 2025 10:36
Show Gist options
  • Save tkoz0/a58dd3bdc4832c57ddf7e5041d3ec5c8 to your computer and use it in GitHub Desktop.
Save tkoz0/a58dd3bdc4832c57ddf7e5041d3ec5c8 to your computer and use it in GitHub Desktop.
reading multiple json objects from a string
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