Skip to content

Developer Tools3 min read

How to Format and Validate JSON for Free

JSON is easy to read when it is formatted and impossible when it is not. Here is how to check whether yours is valid and find the character that broke it.

KLYRO TeamPublished

klyro / tools / json-formatterIn your browserInvalid JSONUnexpected token at line 4, column 18FORMATTED1{2 "order": "A-1042",3 "paid": true,4 "items": [5 { "sku": "A-100", "qty": 2 }6 ]7}Indent24TabMinify

JSON is how nearly every web service sends data. It is also strict in a way that catches people out: one trailing comma or one single quote and the whole document is rejected.

A formatter does two things. It tells you whether the JSON parses at all, and if it does, it lays it out so you can read it.

The rules JSON actually enforces

Almost every JSON error people hit is one of five things. It is worth knowing them by sight.

  • Double quotes only. 'name' is not valid. Neither is a key without quotes.
  • No trailing comma. A comma after the last item in an object or array is an error, even though JavaScript allows it.
  • No comments. // and /* */ are not part of JSON, however many configuration files pretend otherwise.
  • Numbers are plain. No leading zeros, no +, no NaN and no Infinity.
  • Only six types. String, number, boolean, null, object and array. A date is a string, and it is up to both ends to agree on its format.

How to find what is wrong

  1. Paste the whole document

    Not a fragment. A parser cannot judge a piece of an object, and most "invalid JSON" reports turn out to be half a document.

  2. Read the line and column

    The error gives you a position. The mistake is normally on that line or the one directly above it.

  3. Look for the five rules above

    In practice it is a trailing comma or a single quote about nine times out of ten.

Invalid JSONUnexpected token ’}’ at line 5, column 11{2 "order": "A-1042",3 "paid": true,4 "total": 42.00,5}trailing comma
The reported position is where the parser gave up, which is usually just after the actual mistake.

Formatted or minified

FORMATTED{ "order": "A-1042", "paid": true, "items": [ { "sku": "A-100" } ]}214 bytes · easy to readMINIFIED{"order":"A-1042","paid":true,"items":[{"sku":"A-100"}]}142 bytes · smaller over the wireSame data, either way
Identical data. Formatting is for people; minifying is for the wire.

Formatting adds indentation and line breaks so a human can follow the structure. Minifying strips every space that is not inside a string, which makes the payload smaller to send.

Neither changes the data. You can go back and forth as often as you like and get the same document out.

Reading a nested structure

Once formatted, indentation does the explaining. A key at two spaces is inside the top-level object; a key at six spaces is inside an object inside an array inside that.

{
  "order": "A-1042",
  "paid": true,
  "customer": {
    "name": "Northgate Supplies",
    "country": "GB"
  },
  "items": [
    { "sku": "A-100", "qty": 2 },
    { "sku": "A-102", "qty": 1 }
  ]
}
Two nested objects and an array of objects. The indentation is the whole map.

Common mistakes

  • Copying JSON out of a terminal that wrapped the lines, and pasting the wrapping with it.
  • Pasting a fragment rather than the whole document and reporting the parser as broken.
  • Adding comments to a .json configuration file. Some tools tolerate it; the standard does not.
  • Escaping quotes by hand inside a string when the value already came escaped. Two rounds of escaping produces something that parses and is wrong.
  • Pasting a payload containing real customer data into an online formatter without checking where the formatting happens.

Questions

Is my JSON sent to Klyro?
No. It is parsed and reformatted by your own browser, which is why it is safe to paste a real API response into it.
Why does my JSON fail when it looks fine?
Nine times out of ten it is a trailing comma or a single quote. An invisible character copied from a document or a chat window is the usual tenth.
Does formatting change my data?
No. Formatting and minifying only change whitespace. Parse either version and you get the same values.