The Most Common JSON Syntax Errors and How to Fix Them
JSON looks like JavaScript, and that resemblance causes most of its errors. Many habits that are perfectly fine in JavaScript, Python or YAML are forbidden in JSON, whose grammar is deliberately tiny and strict. A single stray comma is enough for a parser to reject the whole document.
Error messages differ from one parser to another, and browsers often word them vaguely, such as “Unexpected token” or “Expected double-quoted property name”, along with a position. The position usually points at, or just after, the real mistake. The sections below cover the errors that cause the vast majority of those messages, each with a short wrong and right example.
Trailing and missing commas
In JSON, commas separate items; they never end a list. JavaScript and many other languages accept a comma after the last element, which makes editing easier, but JSON does not. The reverse mistake, forgetting a comma between two items, is just as common when lines are added or moved by hand.
- Wrong: {"name": "Ada", "year": 1815,} Right: {"name": "Ada", "year": 1815}
- Wrong: [1, 2, 3,] Right: [1, 2, 3]
- Wrong: {"a": 1 "b": 2} Right: {"a": 1, "b": 2}
Quotes: single quotes, unquoted keys and curly quotes
JSON has exactly one kind of string delimiter: the straight double quote. Single quotes are invalid, and every object key must be a double-quoted string, even when it is a simple word. This error is frequent when someone copies a JavaScript object literal or the output of Python's print function, which uses single quotes.
A subtler version comes from word processors, chat apps and some note-taking tools, which automatically turn straight quotes into typographic “curly” quotes. They look almost identical on screen but are different characters, and a parser rejects them.
- Wrong: {'name': 'Ada'} Right: {"name": "Ada"}
- Wrong: {name: "Ada"} Right: {"name": "Ada"}
- Wrong: {“name”: “Ada”} Right: {"name": "Ada"}
Comments are not allowed
Standard JSON has no comment syntax, neither // nor /* */. Some tools accept comments in their own configuration files, using variants such as JSONC or JSON5, which is why it is easy to assume they are always fine. A strict parser such as JSON.parse will fail on them.
If you need to document a value inside the data itself, use an ordinary key that your program ignores, or keep the explanation in separate documentation.
- Wrong: {"port": 8080 // default port} Right: {"port": 8080}
- Alternative: {"_comment": "8080 is the default port", "port": 8080}
Values JSON does not have: NaN, Infinity, undefined
JSON values are limited to strings, numbers, true, false, null, objects and arrays. NaN, Infinity, -Infinity and undefined are JavaScript concepts with no JSON equivalent, and writing them in a document makes it invalid. Python's json module can write NaN and Infinity by default, so files produced that way may fail elsewhere.
Watch the other direction too: JavaScript's JSON.stringify does not fail on these values, it silently converts NaN and Infinity to null and drops properties whose value is undefined. Numbers also have a strict format: no leading zeros, no plus sign, no hexadecimal.
- Wrong: {"ratio": NaN} Right: {"ratio": null}
- Wrong: {"middleName": undefined} Right: {"middleName": null}, or leave the key out
- Wrong: {"code": 007} Right: {"code": 7}, or {"code": "007"} if the zeros matter
- Wrong: {"mask": 0xFF} Right: {"mask": 255}
Unescaped characters inside strings
Inside a string, three things must be escaped with a backslash: the double quote, the backslash itself, and control characters, which include the line break and the tab. A string therefore cannot span several lines in the file; a line break inside it must be written as \n. This error often appears when a multi-line text, an HTML snippet or a Windows path is pasted into a JSON value by hand.
- Wrong: {"quote": "She said "hello""} Right: {"quote": "She said \"hello\""}
- Wrong: a string that starts with "first line and continues on the next line of the file with second line" Right: {"text": "first line\nsecond line"}
- Wrong: {"path": "C:\Users\Ada"} Right: {"path": "C:\\Users\\Ada"}, or use forward slashes: "C:/Users/Ada"
- Wrong: a literal tab character inside a string Right: \t
Big integers that lose precision in JavaScript
This one is not a syntax error, which makes it dangerous: the document is valid and parses without complaint, but the value changes. JavaScript stores every number as a 64-bit floating-point value, which can represent integers exactly only up to 9007199254740991, the value of Number.MAX_SAFE_INTEGER. Above that, integers are rounded to the nearest representable value.
For example, JSON.parse on {"id": 9007199254740993} returns an object whose id is 9007199254740992. If that id is a database key, an order number or a social network post ID, you end up reading or updating the wrong record without any error. 64-bit identifiers generated by databases and distributed systems often exceed this limit.
The standard solution is to send such values as strings: {"id": "9007199254740993"}. Many public APIs already do this, sometimes offering a separate string field next to the numeric one. Other languages, such as Python or Java with the right types, can read these integers exactly, so the problem may only surface once the data reaches a browser or a Node.js service.
How to locate an error quickly
- Read the position in the message: go to that line and column, then look just before it. A missing comma or quote is usually detected one token later.
- Pretty-print the document first. In a single line of 50,000 characters, an error is invisible; formatted with indentation, it usually stands out.
- Check the end of objects and arrays for trailing commas, and check every copy-pasted string for quotes and line breaks.
- If a document seems to lose values after formatting, look for duplicate keys: with JSON.parse, the last occurrence wins and the earlier one disappears.
- The Toolvance JSON formatter validates your document in the browser, reports the line and column of the first error, highlights the faulty line and warns about integers beyond 9007199254740991, without sending your data anywhere.