Build up a list of dictionaries parsing each character. One could also parse each line.
There is good probability of finding a user library that already does this function but here is a way to go
import json
braces = []
dicts = []
dict_chars = []
for line in inp: # input is a builtin so renamed from input to inp
char = line.strip()
dict_chars.append(line)
if '{' == char:
braces.append('}')
elif '}' == char:
braces.pop()
elif len(braces) == 0 and dict_chars:
text = ''.join(dict_chars)
if text.strip():
dicts.append(json.loads(text))
dict_chars = []
Then, merge dictionaries in the list.
merged_dict = {}
for dct in dicts:
merged_dict.update(dct)
> print(merged_dict)
{u'a': {u'x': u'y', u'w': u'z'}, u'b': {u'z': u'l', u'v': u'w'}}
Output merged dictionary as json string with indentation.
merged_output = json.dumps(merged_dict, indent=4)