⚡ SmartDevTools

Common JSON Syntax Errors and How to Fix Them

By SmarTech Apps · Published 2026-11-18 · 6 min read

JSON is strict on purpose. A parser either accepts a document or refuses it, and the error it prints is often terse. Almost every failure comes from a short list of mistakes, usually made by people who write JSON the way they write JavaScript, Python or YAML. This guide walks through that list with before and after examples and explains how to read the messages.

Trailing commas

A comma after the last item in an object or array is legal in JavaScript but not in JSON. It is the single most common error, usually left behind after deleting the last line of a list.

// before
{ "name": "Ada", "roles": ["admin", "dev",], }

// after
{ "name": "Ada", "roles": ["admin", "dev"] }

Node 25 reports the first case as Expected double-quoted property name in JSON at position 7. The parser read the comma, expected another key, and found the closing brace instead.

Single quotes and unquoted keys

JSON strings and keys must use double quotes. Single quotes and bare keys are valid in JavaScript object literals and Python dictionaries, so pasting from a console or a log line produces invalid JSON.

// before
{ 'user': 'ada', id: 7 }

// after
{ "user": "ada", "id": 7 }

Modern V8 prints Expected property name or '}' in JSON at position 1 for both, pointing at the first character that is not a double quote. A Python repr adds another trap: True, False and None must become true, false and null.

Comments

JSON has no comment syntax, neither // nor /* */. Files such as tsconfig.json accept comments only because their own tools use a more lenient parser called JSONC. Send the same file to a standard parser and it fails. Either strip the comments, or store the note as data, for example a "_comment" key, if consumers will ignore unknown keys. If you need real comments, YAML supports them, as discussed in JSON vs YAML vs CSV.

NaN, Infinity and undefined

JSON numbers are finite decimals. NaN, Infinity and undefined are not values in the grammar. JSON.stringify quietly turns NaN and Infinity into null and omits keys whose value is undefined, which can hide data loss, while other serialisers such as Python's json.dumps will happily emit a bare NaN that browsers then reject.

// before
{ "score": NaN, "limit": Infinity }

// after: choose null, or a string, and document it
{ "score": null, "limit": null }

Chrome and Node report the first as Unexpected token 'N', "{"score":NaN}" is not valid JSON.

Bad escapes and raw control characters

Inside a string, a backslash must be followed by one of " \ / b f n r t or uXXXX. Windows paths are the classic offender, because "C:\temp\new" must be written with doubled backslashes. A literal newline or tab inside a string is also invalid: use \n.

// before
{ "path": "C:\data\q1" }

// after
{ "path": "C:\\data\\q1" }

Here \q is not a legal escape, and Node says Bad escaped character in JSON at position 8. If a JSON string is itself embedded in another string, you may need to escape twice, which is easier to get right with a dedicated tool than by hand.

Numbers: leading zeros, plus signs, hex

JSON allows 0 and 0.5 but not 007, +5, .5, 5. or 0xFF. Identifiers with leading zeros should be strings, because a number cannot carry them.

// before
{ "zip": 01234, "mask": 0xFF }

// after
{ "zip": "01234", "mask": 255 }

The error looks like Unexpected number in JSON at position 6, which is confusing because the number looks fine to a human. The leading zero is the problem.

Duplicate keys

The specification says object names should be unique but does not make duplicates a syntax error, so parsers do not complain. Most keep the last value and silently discard earlier ones. Because the file still parses, this slips through. Comparing a file with an edited copy in JSON Compare, or scanning the formatted output, is the practical way to catch it, as is a linter that flags duplicates.

Byte order marks and wrong encodings

Some Windows editors save UTF-8 with a BOM, the three bytes EF BB BF. The result begins with an invisible character, so the parser fails at position 0 with a message like Unexpected token even though the file looks perfect. Re-save as UTF-8 without BOM, or strip a leading \uFEFF before parsing. JSON should always be UTF-8.

Truncated payloads and wrong content

If the document ends before its braces close, you get Unexpected end of JSON input or, in newer engines, Expected ',' or '}' after property value. Typical causes are a response cut off by a size limit, a copy that missed the last line, or a log line that was wrapped. Another variant is HTML where JSON was expected: an error page beginning with < produces Unexpected token '<', which means the server returned an HTML error and you should check the status code and URL before touching the JSON.

Reading parser messages

Treat the reported position as the place the parser gave up, not necessarily where you made the mistake. A missing comma is reported at the start of the next token, and an unclosed string is reported far downstream. Chrome and Node count characters from zero, newer versions add a line and column, and older ones give only an offset. Look one token before the position. Older engines print Unexpected token } in JSON at position 42, which is the same trailing-comma problem described above.

  1. Pretty-print the document so structure is visible.
  2. Go to the reported line and read backwards for a missing comma, quote or brace.
  3. Fix one error at a time, since fixing one often moves the next error forward.

Using our tools to repair invalid JSON

The JSON Formatter validates as you format and reports invalid input, and it has a Repair JSON button. That repair is best-effort and deliberately simple: it removes trailing commas, converts single quotes to double quotes and quotes bare keys, then re-parses. If the result is still invalid it shows the error instead. Be aware that converting every single quote means an apostrophe inside a value can break the output, so review the result.

The JSON Editor has a more thorough Auto-Fix button. In the code, it strips a BOM, removes comments, fixes single quotes, quotes unquoted keys and values, replaces Python and JavaScript literals with JSON ones, removes duplicate and trailing commas, and adds missing commas between values. If the input is already valid it simply formats it. Auto-fix guesses, so it cannot recover truncated data or know what a missing value should have been. Always compare the repaired output with your source before using it, and when a server generated the broken JSON, fix the producer rather than repairing its output every time.

Try the tools

Keep reading