I have string
input_str = '{ "key1": 123, "key2": "val" }, { "key3": 345, "key4": {"key5": "val"} }'
I would like to split it into list by outermost curly brackets:
input_list = ['{ "key1": 123, "key2": "val" }', { "key3": 345, "key4": {"key5": "val"} }]
I wrote this code to obtain it:
input_str = '{ "key1": 123, "key2": "val" }, { "key3": 345, "key4": {"key5": "val"} }'
input_list = []
counter = 0
current_str = ''
for char in input_str:
if char == '{':
counter += 1
if char == '}':
counter -= 1
if counter == 0:
if current_str:
current_str += char
input_list.append(current_str)
current_str = ''
else:
current_str += char
print(input_list)
Is there any more pythonic way to do it?