← All blog posts

How to get an accurate result from the JSON Formatter

WhiteUI · A plain-English guide to the JSON Formatter

An accurate result from the JSON Formatter comes from two habits: pasting the complete text you mean, and checking the key count before you copy the output. The tool already uses exact parsing, so most accuracy problems come from a paste that was cut short, a key that was typed wrong, or an error message that was read too quickly. The five checks below take less than a minute together.

At a glance

Trailing comma fails at position 8059{"a": 1,} is 9 characters long
The input {"a": 1,} is 9 characters long, and the trailing comma fails at position 8, the closing brace.

Paste the whole thing

Copy the full block, from the opening brace to the closing brace, and check that the pasted text ends where the original ends. A long block copied from a chat message or a terminal can get cut off, so the closing brace never arrives, and the tool reports an error at the last character. Before you paste, look at the end of the text: the sample ends with tools_count": 2}, and if your paste ends in the middle of a value, it is incomplete. Paste again, slowly, and the error disappears.

Check the key count

Read the first small line under the big number: it names the count of top-level keys. For the sample it must read “4 top-level keys”, for the weather example it must read 3, and for the monthly budget it must read 4. If the count is lower than you expect, a key is missing, which usually means the paste was cut short or a key was deleted while editing. If the count is higher, a key may be duplicated, which happens when a block is pasted twice by accident. The count is the fastest check the tool gives you, and it takes one glance.

Read the error position

When the tool reports invalid JSON, the error names a position, and that position is exact. In {"a": 1,}, which is 9 characters long, the trailing comma fails at position 8, the closing brace. In {oops}, which is 6 characters long, the missing quotes fail at position 1, the second character. Count characters from the start, with the first character at position 0, and look at the spot the error names: the fault is there, or one character before it. Fixing the named spot is quicker than rewriting the whole block from scratch.

Compare the counts

The second small line compares the original and formatted lengths, and the two numbers should make sense together. For the sample, Pretty reads “Original: 98 characters · Formatted: 120 characters”, a gain of 22 characters of spaces and line breaks, and Compact reads “Original: 98 characters · Formatted: 90 characters”, a loss of 8 characters of spaces. If the formatted count is shorter than the original in Pretty mode, something was dropped from the input, because Pretty only ever adds whitespace. If the two counts are identical, the input may already have been compact, which is fine, but worth a glance.

Worked check: the order example

Check any result with the order example, because it has numbers you can verify. Paste {"order": 4517, "items": [{"name": "Notebook", "price": 120}, {"name": "Pen", "price": 45}], "paid": true}. The panel must read 162 characters in Pretty mode, across 14 lines, and 94 characters in Compact mode. Then read the values: the order number must still be 4517, the two prices must still be 120 and 45, and paid must still be true. If all three values are right and the counts match, the formatting is exact, and the same check works for any input you paste afterwards.

The common mistake

People read an error, guess at the fault, and edit a different part of the text, so the error stays and the text gets worse. The error message names a position for a reason: in {"a": 1,} the fault is at position 8, not at the start, and in {oops} it is at position 1, not at the end. Go to the named position first, fix what is there, and only then look elsewhere if the error remains. One precise fix beats three guesses.

Frequently asked questions

How do I get an exact result from the JSON Formatter?

Paste the complete text, then check the top-level key count: the sample must read 4 top-level keys, and the counts must match the expected lengths.

What should the order example read?

162 characters across 14 lines in Pretty mode, and 94 characters in Compact mode, from a 106-character input, with prices 120 and 45 unchanged.

How do I read an error position?

Count characters from the start, with the first character at position 0. In {"a": 1,} the trailing comma fails at position 8, the closing brace.

Why is my Pretty output shorter than my input?

It should not be: Pretty only adds whitespace. A shorter Pretty output means something was dropped from the input, usually a cut-off paste.

More guides on the JSON Formatter

Try it yourself

Try the JSON Formatter — free, in your browser.

Open the JSON Formatter