Last active
August 29, 2015 14:06
-
-
Save nir0s/1231027fa165e80c7079 to your computer and use it in GitHub Desktop.
Parses a string and returns a list of JSON's
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
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