Common JSON errors and how to fix them

Updated:

JSON is the format many APIs, configuration files and web apps use to exchange data, and its grammar fits on a single page. That is also why it is unforgiving: a text is either valid JSON or it is not, and one stray comma is enough for a parser to reject the whole thing. Most errors come from the same few habits, many of them carried over from JavaScript or other languages. This guide lists the rules, the mistakes that break them most often, how to read the error message, and a few cases that are valid JSON but still cause problems.

The rules JSON actually has

The current definitions are RFC 8259, published in December 2017, and ECMA-404. They describe the same syntax:

Between tokens, only four whitespace characters are allowed: space, tab, line feed and carriage return.

The most common errors

Each of these is rejected by a standard parser. The fix is in the description.

JSON formatter & converter

Reading the error message

A parser reads the text from left to right and stops at the first character it cannot accept, so the position in the error message is where it gave up, which is often just after the actual mistake. With a trailing comma, the parser complains about the closing brace, because after a comma it expects another property name. With a missing bracket, the error may appear at the very end of the text, far from where the bracket should have been.

The wording differs between browsers and languages, and many messages give a position, though not all, either as a character count from the start or as a line and column. For example, Chrome's JavaScript engine reports {"name":"Ana",} as:

When the position is not obvious, look at the character just before it, and check that every opening bracket and quote before that point has its partner. An editor that highlights matching brackets often makes a missing one easy to spot.

Valid JSON that still causes trouble

JSON is not JavaScript

JSON's syntax was taken from JavaScript object literals, which is why the two are so easily confused. A JavaScript object literal accepts single quotes, unquoted names, trailing commas, comments and values such as undefined; JSON accepts none of them. Code that works when pasted into a script can therefore fail as a JSON file.

Some tools accept extended formats such as JSON5, or “JSON with comments” for configuration files. They are convenient where they are supported, but they are not JSON, and a standard parser, including the nTools formatter, will reject them.

Frequently asked questions

Can JSON have comments?

No. The specification has no comment syntax. If you need notes in the data, put them in a property such as "_comment", or use a format that allows comments where the tools support it.

Why does my JSON work in JavaScript but fail in a validator?

Because a JavaScript object literal allows things JSON does not, such as single quotes, trailing commas and unquoted names. Pasted into code it runs; saved as JSON it is invalid.

How do I keep a very large number exact?

Send it as a string, for example "9007199254740993", and convert it on the receiving side with a type that can hold it, such as BigInt in JavaScript.

Does the nTools formatter send my JSON anywhere?

No. It is parsed and formatted in your browser, and when the browser reports a position, the formatter shows it as a line and column.

Related guides