Skip to content

Instantly share code, notes, and snippets.

@nir0s
Last active August 29, 2015 14:06
Show Gist options
  • Save nir0s/1231027fa165e80c7079 to your computer and use it in GitHub Desktop.
Save nir0s/1231027fa165e80c7079 to your computer and use it in GitHub Desktop.
Parses a string and returns a list of JSON's
def convert_string_to_json_list(string):
"""This will receive a string as an input, count curly braces and ignore
any newlines. When the curly braces stack is 0, it will append the
entire string it has read up until then to a list and continue.
:param string: the string to parse
:rtype: list of JSON's
"""
stack = 0
json_list = []
tmp_json = ''
for char in string:
if not char == '\r' and not char == '\n':
# build the current json
tmp_json += char
# stack counting...
if char == '{':
stack += 1
elif char == '}':
stack -= 1
if stack == 0:
# if the string wasn't empty...
if not len(tmp_json) == 0:
# add it
json_list.append(tmp_json)
# and move on to the next one
tmp_json = ''
return json_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment