← All blog posts

JSON Formatter formula — how it actually works

WhiteUI · A plain-English guide to the JSON Formatter

Every result from the JSON Formatter comes from two short operations, and there is no hidden step in between. The tool reads your text and turns it into a value, then it prints that value again, either spread across lines or squeezed onto one. The two operations below are the whole method.

Pretty = JSON.stringify(value, null, 2)
Compact = JSON.stringify(value)
Invalid JSON = the parser error, shown as-is

At a glance

Depth 3 sits 6 spaces in0 spaces5 spaces10 spaces“c”: 1 sits 6 spaces in
The deepest value in the nested example, “c”: 1, sits 6 spaces in, because each level of nesting adds 2 spaces.

Step one: parse the text into a value

The first operation is JSON.parse, which reads the text in the box and turns it into a value the browser can work with: an object, a list, a number, or a piece of text. For the sample the value is an object with four keys. If the text is not valid JSON, this step fails, and the tool shows the parser’s error instead. For {oops} the error is “Expected property name or ‘}’ in JSON at position 1 (line 1 column 2)”, which points at the second character, where a quoted key name was expected. For {"a": 1,} it is “Expected double-quoted property name in JSON at position 8 (line 1 column 9)”, which points at the closing brace after the trailing comma.

Step two: print the value again

The second operation is JSON.stringify, which prints the value back as text. Pretty mode calls it with two extra arguments, null and 2, which mean no key filtering and two spaces of indent per level. Compact mode calls it with no extra arguments, which means no indent and no line breaks at all. That one number, 2, is the entire difference between the two buttons. The sample proves it: Pretty gives 120 characters across 9 lines, Compact gives 90 on one line.

Reading the indent

The number 2 means two spaces per level of nesting, and the levels add up. A key at the top level sits two spaces in, a value inside a list sits four spaces in, and a value inside an object inside a list sits six spaces in. In the order example the prices 120 and 45 sit six spaces in, because they are at the third level: inside the order object, inside the items list, inside one item object. In the deep example, {"a": {"b": {"c": 1}}}, the value 1 sits six spaces in for the same reason, and the whole output is 46 characters across 7 lines, from a 22-character input.

What the counts in the result panel mean

The big line shows the length of the formatted text: “120 characters” for the sample in Pretty mode, “90 characters” in Compact mode. The line under it names the mode and the key count: “Valid JSON · 4 top-level keys · Mode: Pretty”. The next line compares the two lengths: “Original: 98 characters · Formatted: 120 characters”. Those three numbers are enough to check the whole operation.

Worked check: count the lines yourself

Take the sample and count the lines of the Pretty output by hand. The opening brace is line 1, the name line is line 2, the tools line is line 3, the two tool names are lines 4 and 5, the closing bracket is line 6, the free line is line 7, the tools_count line is line 8, and the closing brace is line 9. Nine lines, which matches the tool. Then count the Compact output: one line, 90 characters. If your hand count lands on 9 lines and 90 characters, you have the method right.

The common mistake

People assume the tool reorders their keys, because the output looks tidier than the input. It does not: JSON.stringify prints keys in the order they appear in the parsed value, which is the order they appear in your text. The sample prints name first and tools_count last, because that is the order they were pasted in. If your keys come out in a different order from the one you expected, the input itself was in that order.

Frequently asked questions

What is the formula behind the JSON Formatter?

Parse with JSON.parse, then print with JSON.stringify. Pretty uses JSON.stringify(value, null, 2), with 2 spaces of indent per level; Compact uses JSON.stringify(value), with no indent.

How deep does the indent go?

Two spaces per level: top-level keys sit 2 spaces in, values inside a list sit 4 spaces in, and values inside an object inside a list sit 6 spaces in.

What does the error for {oops} mean?

The parser expected a quoted key name and found something else at position 1, the second character. The error names the exact position, so you know where to look.

Does formatting reorder my keys?

No. Keys print in the order they appear in your input. The sample prints name first and tools_count last, because that is the pasted order.

More guides on the JSON Formatter

Try it yourself

Try the JSON Formatter — free, in your browser.

Open the JSON Formatter