8 min read

Common JSON syntax errors

Trailing commas, bad quotes, comments, unclosed brackets, and undefined values are the JSON mistakes developers hit most often.

Table of contents

  1. Trailing commas
  2. Quotes and object keys
  3. Comments and invalid values
  4. Brackets and escaping

Trailing commas

Trailing commas are probably the most common JSON error. They happen when the final property or array item ends with a comma. JavaScript allows trailing commas in many contexts, but JSON does not.

Fixing this is simple: remove the comma after the last item. If the same data came from a JavaScript object, check for other JavaScript-only syntax too.

Quotes and object keys

JSON requires double quotes around string values and object keys. Single quotes, smart quotes, and unquoted keys are invalid. This often appears when copying from JavaScript examples or rich text documents.

Replace single quotes with double quotes and make sure every key is quoted. Be careful with automated replacements if string values contain apostrophes.

Comments and invalid values

Standard JSON does not allow comments. It also does not allow undefined, NaN, Infinity, functions, dates, or regular expressions as native values. Use null or strings when a value needs to be represented in JSON.

If you need comments in a config file, consider whether the file is actually JSONC, YAML, or another format rather than JSON.

Brackets and escaping

Unclosed objects, unclosed arrays, and broken escape sequences can be harder to spot in dense JSON. Formatting helps only after the document is valid, so use a validator first. Parser positions often point near the issue.

Inside strings, backslashes must form valid escape sequences. File paths, regular expression-like text, and copied command strings are common sources of escaping mistakes.

Related guides

FAQ

Why does JSON reject single quotes?

The JSON specification requires double quotes for strings and keys.

Are trailing commas valid JSON?

No. Remove commas after the final array item or object property.

Can JSON include comments?

No. Comments are not part of standard JSON syntax.

What is the fastest way to find an error?

Use a JSON validator, fix the first reported issue, and validate again.