How to Format and Validate JSON, and Fix the Most Common Errors

What valid JSON looks like, why parsers reject it, and how to fix trailing commas, single quotes, unquoted keys and other frequent mistakes.

By Utkarsh Garg · 2026-10-27 · 3 min read

What JSON allows

JSON is a small text format with strict rules. A document is a single value: an object in curly braces, an array in square brackets, a string, a number, true, false or null. Objects hold key and value pairs, and every key must be a string wrapped in double quotes.

That strictness is why JSON is easy for programs to read, and also why a single stray character makes a parser reject the whole document. JSON is not JavaScript. Several things that are fine in a JavaScript object literal are not valid JSON.

The mistakes that cause most parse errors

Most broken JSON comes from a short list of habits carried over from code or from hand editing.

  • Trailing commas. A comma after the last item in an object or array is not allowed, so {"a": 1,} fails.
  • Single quotes. Strings and keys must use double quotes, so {'a': 1} fails.
  • Unquoted keys. {a: 1} works in JavaScript but not in JSON.
  • Comments. JSON has no comment syntax, so // note and /* note */ both break parsing.
  • Unescaped characters inside strings. A double quote, a backslash or a raw line break inside a string must be escaped as \", \\ or \n.
  • Values that JSON does not have: undefined, NaN, Infinity and functions. Use null or a string instead.
  • A byte order mark or stray text before the first brace, often added by a text editor or copied from a log line.

Reading an error message

Most parsers report a position or a line and column. The reported spot is where the parser gave up, which is usually at or just after the real mistake. If a message says unexpected token at the start of a key, look at the character before it for a missing comma or an extra one. If it says unexpected end of input, a closing brace or bracket is missing somewhere earlier.

Fix problems from the top of the document downward. One missing quote can make everything after it look wrong, so later errors often disappear once the first one is fixed.

Formatting and minifying

Formatting, also called pretty printing, adds line breaks and indentation so the structure is easy to scan. It does not change the data. Two or four spaces are the usual choices, and teams generally pick one and keep it so diffs stay small.

Minifying does the opposite and removes every space and line break that is not inside a string. The result is smaller, which helps when JSON is sent over a network or stored in a size limited field. Neither operation changes keys, values or their order.

A quick checking routine

When a payload is rejected, paste it into a formatter and read the first error. Fix it, run it again and repeat until the document formats cleanly. Then confirm the shape matches what the receiving system expects, because valid JSON can still have the wrong field names or types. A formatter that runs in your browser keeps the data on your machine, which matters when the payload contains tokens or customer records.