f_in = open("in.txt", "r")
f_out = open("out.txt", "w")
INDENT = 4
brace_count = 0#number of {
bracket_count=0
write_for_brakcet = False
for line in f_in:
for i in range(len(line)):
if write_for_brakcet:
if line[i]==']':
write_for_brakcet = False
f_out.write(line[i])
continue
if line[i]=='{':
brace_count = brace_count + 1
f_out.write('{\n'+' '*INDENT*(brace_count+bracket_count))
elif line[i]=='[':
if line[i+1]=='{':
bracket_count = bracket_count + 1
f_out.write('[\n'+' '*INDENT*(brace_count+bracket_count))
else:
write_for_brakcet = True
f_out.write('[')
elif line[i]=='}':
brace_count = brace_count - 1
if i<len(line)-1 and line[i+1]==',':
f_out.write('}')
elif i<len(line)-1 and line[i+1]==']':
f_out.write('}\n'+' '*INDENT*(brace_count+bracket_count-1))
else:
f_out.write('}\n'+' '*INDENT*(brace_count+bracket_count))
elif line[i]==']':
bracket_count = bracket_count - 1
if i<len(line)-1 and line[i+1]=='}':
f_out.write(']\n'+' '*INDENT*(brace_count+bracket_count-1))
else:
f_out.write(']\n'+' '*INDENT*(brace_count+bracket_count))
elif line[i]==',':
if line[i+1]=='}':
f_out.write(','+' '*INDENT*(brace_count+bracket_count))
else:
f_out.write(',')
elif line[i]==' ' and line[i-1]==',':
f_out.write('\n'+' '*INDENT*(brace_count+bracket_count))
elif i<len(line)-1 and line[i+1]=='}':
f_out.write(line[i]+'\n'+' '*INDENT*(brace_count+bracket_count-1))
else:
f_out.write(line[i])
f_in.close()
f_out.close()