gg
Home › Blog › How to read nested JSON by counting brackets

How to read nested JSON by counting brackets

2026-10-17 · 2 min read
devdataweb

JSON looks intimidating minified because the only thing separating one value from the next is punctuation, and your eye has nowhere to rest. The fix isn't a trick — it's one rule, applied consistently: every time you see { or [, indent one more level; every time you see } or ], indent one less level. Depth is just a running count of opens minus closes.

The method

  1. Start a depth counter at 0.
  2. Read the string left to right. On { or [, print a newline, then depth × 2 spaces (two spaces per level is the common convention), then the bracket, then increase depth by 1.
  3. On } or ], decrease depth by 1 first, then print a newline and depth × 2 spaces before the bracket.
  4. On a comma inside an object or array, print a newline and indent to the current depth before the next key or value.
  5. Everything else — keys, strings, numbers, booleans — prints as-is on the current line.

A worked example

Take this minified object: {"brand":"gg","tools":250,"live":true}.

Walking it: { opens depth 0 → 1. Then three key-value pairs at depth 1, each getting 1 × 2 = 2 spaces of indent, separated by commas and newlines. Then } drops depth 1 → 0 and prints at 0 spaces. The pretty-printed result is:

Four characters of punctuation became five lines, and the structure — one object, three keys, no nesting — is now visible at a glance instead of inferred from commas.

Where the bracket count catches real bugs

The same counter that drives indentation is also how you spot malformed JSON before a parser does. If you reach the end of the string and your depth counter isn't back to 0, a bracket is missing — and the running count tells you roughly where, because depth should return to 0 exactly when the outermost value closes. A trailing comma before a closing bracket, a stray quote that swallows the rest of the line, or one extra } copy-pasted from another block are the three mistakes that show up this way most often, and all three are invisible in a minified blob but obvious once indentation makes the depth visible.

Minifying is the same rule run backward: drop every newline and every space that exists purely for indentation, keeping only what the value itself needs (the quotes around strings, the punctuation between them). The content is identical either way — pretty and minified JSON parse to the exact same data, which is the thing worth remembering when a teammate says minifying "changed" something. It didn't; it just removed the whitespace you were using to read it.

The free JSON Formatter runs this same bracket-depth walk in your browser, pretty-printing or minifying on toggle and surfacing the parser's own error message — location included — the moment a bracket doesn't balance. Nothing is uploaded.

Try it free — with the steps shown

The JSON Formatter runs in your browser and shows exactly how it got the answer, so the method sticks.

Open JSON Formatter

More from the blog