I'm trying to create my own JSON formatter in python (yes, i know there are already libraries out there, this is a challenge for myself).
userinput = []
askuser = input("Enter your JSON:\n")
for i in askuser:
userinput.append(i)
for j in userinput:
if '{' in userinput:
userinput.insert(userinput.index('{') +1, '\n')
userinput.insert(userinput.index('\n') +1, '\t')
else:
pass
if '}' in userinput:
userinput.insert(userinput.index('}') -0, '\n')
else:
pass
if ',' in userinput:
userinput.insert(userinput.index(',') +1, '\n')
userinputString = ''.join(userinput)
else:
pass
print(userinputString)
I tried this without looping through, it works, but only when the condition becomes true for the first time. If there are multiple curly brackets, which there usually is in JSON. then this will only format the first one.
My question is... does anyone know why my code just freezes when I input a sample JSON such as this:
{"name":"John", "age":30, "car":null}
Is it due to python just being slow, or am I missing something? (probably the latter)
I notice I'm trying to perform 3 conditional computations on each character in my list, so the bigger it gets the slower it will become, so if this is python I will probably try to write this in C instead, and then make it accessible to python via libraries.