How to Format and Validate JSON (And Fix the Errors You'll Hit)

APIs speak JSON, config files are JSON, and half of debugging is staring at a wall of minified braces wondering where it broke. Here's how to format, validate, and fix JSON like someone who's done it a thousand times.

What "valid" JSON actually requires

JSON looks forgiving. It isn't. The spec is strict, and parsers enforce it ruthlessly. The rules that bite people:

  • Double quotes only. {'name': 'Maya'} is invalid. Keys and strings need ", not '. This is the #1 error from anyone coming from Python or JavaScript.
  • No trailing commas. {"a": 1,} fails. JavaScript allows it; JSON doesn't.
  • No comments. // and /* */ are not part of JSON, even though some tools tolerate them.
  • No undefined, NaN, or Infinity. These are JavaScript-isms, not JSON values.
  • Keys must be quoted. {name: "Maya"} is invalid; {"name": "Maya"} is fine.

Formatting: pretty-print vs. minify

The same JSON can look two ways. Pretty-printed (indented, multi-line) is for humans. Reading, debugging, code review. Minified (all whitespace stripped) is for machines. Smaller payloads over the network.

Rule of thumb: pretty-print anything you'll look at, minify anything you'll transmit. Our JSON formatter does both directions instantly, in your browser. Paste minified API output, get readable JSON back.

The errors you'll actually hit (and their fixes)

"Unexpected token". The classic

Somewhere there's a character the parser didn't expect. The error usually names a position; look just before it. Nine times out of ten it's one of these:

  • A single-quoted string → change to double quotes.
  • A trailing comma after the last item → delete it.
  • An unescaped quote inside a string: "say "hi"" → "say \"hi\"".

"Unexpected end of JSON input"

The document is truncated. A missing closing } or ], often from a cut-off API response or a copy-paste that missed the last line. Paste it into a validator and it'll point at the spot.

Smart quotes

Copying JSON out of Word, Google Docs, Slack, or a blog post silently converts " to curly " ". They're different characters and JSON rejects them. Re-type the quotes or run a find-and-replace.

BOM and invisible characters

A byte-order mark or zero-width space at the start of a file (common with Windows-saved files) makes parsers choke on "unexpected token" at position 0. If position 0 is the complaint and the file looks fine, invisible characters are the culprit.

A practical debugging workflow

  1. Validate first. Paste the JSON into the formatter/validator. It will either pretty-print it (it's valid) or tell you exactly where it breaks.
  2. Bisect large documents. If a 2,000-line config fails, validate halves until the broken half is small enough to eyeball.
  3. Check the boundaries. Most errors live where humans edited: the last item in an object (trailing comma), a hand-typed string (unescaped quote), or pasted content (smart quotes).
  4. Diff against a known-good version. If it worked yesterday, git diff (or any diff) shows exactly what changed.

Validating in code vs. in a tool

In production code, validate with your language's parser and handle errors gracefully:

// JavaScript. Never trust a network response
try {
  const data = JSON.parse(responseText);
  // use data
} catch (err) {
  console.error("Invalid JSON at:", err.message);
}

But for the human side, eyeballing an API response, cleaning up a config, preparing an example for docs, a browser tool is faster than writing a script. Paste, format, fix, copy. The whole loop takes seconds, and since processing happens locally, you're not sending potentially sensitive payloads to a third-party validator.

JSON-adjacent formats worth knowing

  • JSON5. A superset allowing comments, trailing commas, and single quotes. Great for human-written configs; not valid JSON, so don't send it to an API expecting strict JSON.
  • JSONL (JSON Lines). One JSON object per line, ideal for logs and streaming. Each line validates independently.
  • YAML. Common for configs (Docker, CI pipelines). More readable, more footguns. Indentation errors replace brace errors.

Bottom line

JSON errors are almost always one of five things: single quotes, trailing commas, unescaped quotes, smart quotes, or truncation. Validate early with the free JSON formatter, fix at the exact position it reports, and you'll spend your debugging time on real problems instead of punctuation.