Skip to content
TextBench Lab

Developer

JSON Trailing Comma and Other Common JSON Errors

Published 5 min read

On this page
  1. Why does JSON reject things JavaScript accepts?
  2. How do you find the exact line with the error?
  3. Where does a trailing comma error point?
  4. What if single quotes are used?
  5. Do keys need quotes in JSON?
  6. Can JSON contain comments?
  7. How do you spot a missing comma?
  8. How do you confirm the fix worked?
  9. JSON syntax FAQ

A JSON trailing comma error means there is a comma after the last item in an object or array, such as "age": 31,}. JSON allows a comma only between two items, so the parser reads the comma, expects another item, and fails at the closing bracket. Delete that last comma and the error is gone. The same habit from JavaScript causes a handful of related errors, and each one is just as quick to fix once you know where to look.

This guide shows each mistake as broken text, the exact line and column our formatter reports, and the fix.

Why does JSON reject things JavaScript accepts?

JSON looks like JavaScript, so people write it the same way. But JSON is a stricter data format, defined in RFC 8259, and several things that are fine in a JavaScript object are errors in JSON:

Written like thisJavaScript objectJSON
{"a": 1,} (trailing comma)AllowedError
{'a': 1} (single quotes)AllowedError
{a: 1} (unquoted key)AllowedError
// note (comment)AllowedError
{"a": 1}AllowedAllowed

Config files, code snippets and copied examples are the usual source of these habits. If you know the table above, you can spot most problems by eye.

How do you find the exact line with the error?

Paste it and read the line and column in our formatter. Press Validate and the tool shows where the parser stopped, prints that line and puts a caret under the position.

Two things help when reading the result. First, the position is where the parser noticed the problem, which is often just after the real mistake, so look at the character before it as well. Second, a parser reports only the first error. Fix it, validate again, and repeat until the file passes.

Where does a trailing comma error point?

Here is the classic case:

{
  "name": "Ana",
  "age": 31,
}

The formatter reports Invalid JSON at line 4, column 1 and adds the hint "Remove the comma before the closing bracket. JSON does not allow a trailing comma." The position is the closing brace on line 4, not the comma on line 3, because the parser only knows something is wrong when it finds } where it expected another key. Delete the comma after 31.

Arrays follow the same rule. For "tags": ["a", "b",] on line 2, the tool reports line 2, column 21: the closing bracket, again right after the extra comma.

Trailing commas often appear after someone deletes the last item of a list or comments a line out in a code editor, leaving the comma behind.

What if single quotes are used?

JSON strings and keys must use double quotes.

{
  "name": 'Ana'
}

The tool reports line 2, column 11, the opening single quote, with the hint that JSON needs double quotes around strings and keys. Replace each single quote with a double quote. If the text itself contains a double quote, write it as \".

Do keys need quotes in JSON?

Yes. A key in JSON is always a string, and strings need double quotes.

{
  name: "Ana"
}

The tool reports line 2, column 3, the start of name, with the hint "Keys must be in double quotes." Wrap every key in double quotes.

Can JSON contain comments?

No. The JSON grammar has values and whitespace, and nothing for comments, even though many config-file readers accept them.

{
  // user
  "name": "Ana"
}

The result is line 2, column 3, the first slash, with the hint "JSON does not allow comments. Remove them and try again." If you need a note in the data, add an ordinary field such as "note", but only if the program reading the file accepts extra fields.

How do you spot a missing comma?

Every item except the last needs a comma after it. When you add a new line at the end of an object, it is easy to forget the comma on the line above.

{
  "name": "Ana"
  "age": 31
}

The tool reports line 3, column 3, the start of "age", with the hint that a comma may be missing between two items. The comma belongs at the end of line 2. Notice the pattern: a trailing comma has one comma too many, and a missing comma has one too few. Both show up at the item boundary.

For errors that name a strange character, such as a less-than sign, or that report an unexpected end of input, see what an unexpected token in JSON means.

How do you confirm the fix worked?

Validate again. When it passes, the tool reports the top level, for example "Object with 2 keys" for the fixed sample above. If you are not sure what you changed, compare the broken and fixed versions to see the exact characters that differ.

Once the file is valid, check that the keys follow one style; naming keys consistently explains the options. Then the last question is layout: formatting or minifying once it is valid. The check runs in your browser, so nothing you paste is sent anywhere.

JSON syntax FAQ

What is a trailing comma in JSON?

It is a comma after the last item of an object or array, as in [1, 2,]. JSON does not allow it, so remove it.

Why is a trailing comma allowed in JavaScript but not JSON?

They are different specifications. JavaScript's grammar accepts a final comma in object and array literals; the JSON grammar defines the comma only as a separator between two items.

How do I remove comments from JSON?

Delete each comment line, or move the note into a normal field if the receiving program allows it. JSON has no comment syntax.

Can I use single quotes in JSON?

No. Strings and keys must use double quotes.

Why does the error point to the line after my mistake?

The parser reports where it noticed the problem. With a trailing or missing comma, that is the next item or bracket, so check the end of the line above.

Related tools