JSONWeave

JSONWeave / Guides

Processed entirely in your browser

Common JSON syntax errors and how to fix them

By Abhishek Jaiswar ยท Published

JSON has fewer rules than JavaScript, and most invalid documents break one of the same handful. Here is each one, with the fix.

Check your JSON in JSONWeave

Trailing comma

JavaScript allows a comma after the last item. JSON does not. Remove it.

Invalid
{"a": 1, "b": 2,}
Valid
{"a": 1, "b": 2}

Single quotes

Strings and keys must use double quotes.

Invalid
{'name': 'Ada'}
Valid
{"name": "Ada"}

Unquoted keys

Object keys are always strings, so they need quotes. This usually appears when a JavaScript object literal is pasted as JSON.

Invalid
{name: "Ada"}
Valid
{"name": "Ada"}

Missing comma or colon

Items are separated by commas and each key is followed by a colon. A missing one is usually reported at the token after the gap, not at the gap itself.

Invalid
{"a": 1 "b": 2}
Valid
{"a": 1, "b": 2}

Comments

JSON has no comment syntax. Remove // and /* */ comments, or keep them in a format that allows them and strip them before parsing.

Unclosed brackets and strings

Every { needs a } and every [ needs a ]. A missing closing quote makes the parser read on until the next quote, so the reported position can be far from the real mistake. See the guide to truncated JSON when the document simply ends early.

Values that JSON does not have

JSON has no undefined, NaN, Infinity or Date. Use null, a number or a string instead.

Duplicate keys

The same key twice in one object is allowed by the syntax but ambiguous. Most parsers keep the last value and silently drop the first, so treat it as a bug.

Related

Ready to try it on your own JSON?

Open the workspace, paste or drop a file, and nothing leaves your browser.

Check your JSON in JSONWeave