SpunkArt
Data, text and encoding

Validating JSON: the errors that break it

JSON looks like JavaScript, but its grammar is much stricter. One trailing comma or one single quote makes a parser reject the whole file. This page lists the rules, shows the error each break causes, and explains what a validator can and cannot tell you.

Check JSON syntax

The checker reads your text character by character, reports the first error with its line and column, and warns about duplicate keys, which most parsers accept silently. Nothing leaves your browser.

The seven rules people break most

InvalidValidRule
[1, 2, 3,][1, 2, 3]No comma after the last item or member.
{'id': 7}{"id": 7}Strings and keys use double quotes only.
{id: 7}{"id": 7}Every key is a quoted string.
// note or /* note */(remove it)JSON has no comments.
NaN, Infinity, undefinednull or a stringOnly numbers, strings, true, false, null, objects and arrays exist.
007, 0x1F, .5, 5.7, 31, 0.5, 5Decimal numbers only, no leading zeros, a digit on both sides of the dot.
A real tab or line break inside "…"\t or \nControl characters inside strings must be escaped.

Reading an error position

A parser reports where it gave up, which is often one character after the real mistake. In the sample above, the comma after "engines" is fine on its own. The error appears at the ] on line 3, column 30, because a value was expected there. So the habit is: go to the reported position, then look backwards.

  1. Paste the JSON and press Validate.
  2. Jump to the line and column. The caret in the output points at it.
  3. Check the character just before it: a stray comma, a missing comma, or a quote that was never closed.
  4. Fix that one problem and validate again. One early error can hide every later one.

Missing-comma example. In {"a": 1 "b": 2} the parser reads 1, then meets a double quote at column 9 where it expected a comma or a closing brace. The fix is a comma between 1 and "b".

Duplicate keys: valid but risky

The JSON standard says keys should be unique but does not forbid repeats. {"role": "user", "role": "admin"} parses without error in JavaScript and keeps the last value, "admin". Other parsers may keep the first value or reject the document. When two systems read the same file differently, you get bugs that are hard to trace, and sometimes security holes. Treat a duplicate-key warning as an error.

Syntax validation is not schema validation

A syntax check answers one question: can this text be parsed? It cannot tell you that "age": "forty" should have been a number or that a required field is missing. For that you need a schema, a separate document that describes the expected shape. JSON Schema is the common standard. A minimal schema looks like this:

{
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string" },
    "age":  { "type": "integer", "minimum": 0 }
  }
}

Libraries in most languages can check a document against a schema like this one. Use syntax validation while you edit, and schema validation at the boundary where data enters your system.

Questions

Why does my file work in JavaScript but fail here?

A JavaScript object literal allows single quotes, unquoted keys, trailing commas and comments. JSON allows none of them. Files such as tsconfig.json are read by tools that accept comments, but a strict parser does not.

Is a lone number or string valid JSON?

Yes. Current JSON allows any value at the top level, so 42 and "hello" are valid documents. Some old parsers required an object or array.

Does the validator change my data?

No. It only reads the text. To re-indent or minify valid JSON, use the JSON formatter guide.