How to Format and Validate JSON

JSON is strict about syntax. Most errors come from a small set of mistakes that are quick to fix once you can see them.

The rules

An invalid example and its fix

This text has a single-quoted key, a trailing comma and a comment:

{
  'name': "Ada",
  "tags": ["a", "b",],
  // comment
}

Valid version:

{
  "name": "Ada",
  "tags": ["a", "b"]
}

Format or minify?

Format JSON when people need to read it, such as in logs, documentation or debugging. Minify it when size matters, such as in network payloads. Both produce the same data.

Reading error messages

A parser reports where it gave up, which is often just after the real mistake. If an error points to a closing brace, check the line before it for a missing or extra comma. The JSON formatter shows the line and column.

Things to watch for

If you pass JSON to an AI model, it also uses tokens. See How Tokenization Works.