Skip to main content
Convert2JSON

Common JSON syntax errors

Nearly every failed parse is one of a dozen mistakes. Here is each one, what the parser says about it, and the fix.

Last reviewed

Trailing comma

By far the most common. A comma separates items; it never follows the last one. JavaScript, Python and most linters allow it, so it survives copy-and-paste and then fails at the parser.

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

Single quotes

JSON strings are double-quoted, always — keys and values alike. Single quotes usually mean the text came from a Python repr or a JavaScript literal rather than from a real serialiser.

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

Unquoted key

Object keys are strings and must be quoted. An unquoted key is the clearest sign that what you are holding is a JavaScript object literal, not JSON.

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

Missing comma between items

Usually the result of hand-editing or of concatenating two fragments. The parser reports the position where the next item started, which is one token after the real problem — look at the end of the previous line.

Unterminated string

A missing closing quote makes the parser read the rest of the document as one enormous string, so the reported error is usually far below the actual mistake. If the error is at the very end of the file, look for an unclosed quote much earlier.

Comments

JSON has no comments. Configuration files that appear to contain them are JSON5, JSONC or YAML — related formats, different parsers. Strip the comments, or use a parser that expects them.

Invalid
{
  // the customer id
  "id": 12
}

Invalid escape sequence

Inside a string, a backslash must begin one of \" \\ \/ \b \f \n \r \t or \uXXXX. A lone backslash — common in Windows paths — is an error. Write C:\\Users, or the parser stops at the U.

Literal control character in a string

A real newline or tab pasted inside a string is not allowed; they must be written as \n and \t. This happens most often when multi-line text is pasted straight into a value.

NaN, Infinity or undefined

None of these exist in JSON. They appear when a language’s own serialiser was too permissive — Python’s json module emits NaN and Infinity by default, and they are not valid JSON. Use null, or a sentinel string the receiving end understands.

Invalid number

A leading plus (+5), a leading zero (007), a hex literal (0xFF) and a bare decimal point (.5 or 5.) are all invalid. Write 5, 7, 255 and 0.5.

Duplicate key

Valid, but almost never intended. The specification does not say which value wins, and one of them is being thrown away. Worth treating as an error in your own pipeline even though the parser will accept it.

Byte order mark or wrapping text

A UTF-8 BOM before the opening brace makes the first character not {, and some parsers refuse it. The same applies to a JSONP wrapper — callback({...}) — or to log-line noise pasted along with the payload. Trim to the first { or [.

Keep reading

  • How to validate JSONThere are two different questions hiding behind "is this JSON valid": does it parse, and does it match the shape it is supposed to have.
  • JSON escape charactersEight escape sequences plus \uXXXX. Everything else after a backslash is a syntax error.
  • What is JSON?JSON is a text format for structured data. It has six value types, one page of grammar, and a handful of rules that trip people up constantly.