How can I make a tool like

How can I make a tool like JSON Formatter or JSON Viewer for json formatting.

For the string manipulation part, you would parse the JSON and then print it back out.

For example you’d start at the beginning, and perhaps it starts with [, so you will need to create an array, and then you would recursively read each element of that array (each element is valid JSON by itself)
Or if it’s a { you would create an object, for each key-value pair you’d read the key, the colon, and then recursively read the element

Similarly, printing would be checking the type, if it’s a list, then you recursively print each element, first adding [, comma between each and finally a closing ]

If recursion is new to you then that is probably something you’ll need to read up on. It’s stupidly simple but may take a while to click. For example: the total number of branches that a tree branch has, is the sum of total branches for each branch that the branch has. So you could break off each sub-branch and count their branches, and break off theirs and so on.

So JSON can be on one of the forms:

{ string: JSON, string: JSON, ... }
[ JSON, JSON, ... ]

And the places where it says JSON you would just recurse to read/print that
…I might have missed a few types, you’d have to look up what is valid. Numbers might have several different formats that need to be handled… You could treat them as just strings for simplicity’s sake.

1 Like