Skip to content

Guide · Developer

JSON Errors Explained: What Each Message Means and How to Fix It

Trailing commas, single quotes, “Unexpected token <”, NaN from Python: real JavaScript and Python error messages, what causes each and how to fix it.

By OnlineToolPro Editorial TeamPublished 6 min read

The short answer

Most JSON errors come from six mistakes: a trailing comma, single quotes, unquoted keys, comments, a missing comma, or receiving something that isn't JSON at all (an HTML error page, an empty response or undefined). Paste the text into a JSON validator to get the exact line and column, then use the table below to match the message to its cause.

JSON is strict on purpose. It looks like JavaScript, but it accepts far less: no comments, no trailing commas, no single quotes, no undefined or NaN. Anything written by hand — a config file, an API request body, test data — trips over one of these sooner or later.

The frustrating part is the error message, which usually points at the character after the real mistake and is worded differently in every language. Below are the actual messages from JavaScript (Chrome, Edge and Node.js all use the same engine) and Python, mapped to what went wrong.

On this page
  1. Error message lookup table
  2. 1. Trailing commas
  3. 2. Single quotes and unquoted keys
  4. 3. Comments
  5. 4. The response isn't JSON at all
  6. 5. NaN, Infinity and very large numbers
  7. 6. Invisible characters
  8. Finding the error fast
  9. FAQ

Error message lookup table

What JSON parse errors mean in JavaScript (V8) and Python
CauseJavaScript saysPython says
Trailing comma in an objectExpected double-quoted property nameExpecting property name enclosed in double quotes
Trailing comma in an arrayUnexpected token ']'Expecting value
Single quotes or unquoted keyExpected property name or '}'Expecting property name enclosed in double quotes
Missing comma, or a commentExpected ',' or '}' after property valueExpecting ',' delimiter
Empty string or empty responseUnexpected end of JSON inputExpecting value: line 1 column 1 (char 0)
Got an HTML page insteadUnexpected token '<', "<!DOCTYPE "...Expecting value: line 1 column 1 (char 0)
Parsing the value undefined"undefined" is not valid JSON—
Invisible byte-order mark (BOM)Unexpected token ''Unexpected UTF-8 BOM
NaN or InfinityUnexpected token 'N'Accepted (see below)
Raw tab or line break inside a stringBad control character in string literalInvalid control character
What JSON parse errors mean in JavaScript (V8) and Python

JavaScript messages also include a position, like at position 7 (line 1 column 8). Python adds line 1 column 8 (char 7). Newer Python versions word a few of these differently, but the causes are the same.

1. Trailing commas

The single most common JSON error, because JavaScript, Python and most config formats allow it and JSON doesn't:

{
  "name": "Ada",
  "role": "admin",   ← this comma is the problem
}

Notice that the error points at the closing }, not the comma. The parser saw a comma, expected another property, and found the end instead. Whenever an error points at a closing bracket, look one character back.

2. Single quotes and unquoted keys

JSON strings and keys must use double quotes. {'name': 'Ada'} and {name: "Ada"} are valid JavaScript objects but invalid JSON.

This usually happens when someone copies an object from code or prints a Python dictionary with print(data) or str(data) — that produces Python syntax with single quotes. Use json.dumps(data) in Python, or JSON.stringify(data) in JavaScript, to get real JSON.

3. Comments

Standard JSON has no comments at all — neither // line nor /* block */. Some files look like JSON and allow them anyway: VS Code settings and tsconfig.json use a relaxed format often called JSONC. Copy one of those into a strict parser and it fails.

Remove the comments, or move the information into a real field such as "_note": "..." if you need to keep it.

4. The response isn't JSON at all

When code calls an API and then parses the response, the most confusing errors aren't about JSON syntax — the server sent something else:

  • Unexpected token '<' means the response starts with < — almost always an HTML page: a 404, a server error, or a login page because your session expired.
  • Unexpected end of JSON input means the body was empty, which is normal for a 204 No Content response or a request that failed silently.
  • "undefined" is not valid JSON means you passed undefined — often from reading localStorage or a property that doesn't exist.
  1. 1
    Check the HTTP status code before parsing.
  2. 2
    Check the Content-Type header is application/json.
  3. 3
    Log the first couple of hundred characters of the raw text — you'll see at once whether it's HTML or empty.

5. NaN, Infinity and very large numbers

Here's a trap that catches teams with a Python backend and a JavaScript frontend. Python's json module writes NaN and Infinity by default:

json.dumps({"score": float("nan")})   →   {"score": NaN}

That isn't valid JSON, and JSON.parse in the browser rejects it with Unexpected token 'N'. Pass allow_nan=False to json.dumps to catch it on the server, and send null instead.

Large IDs can change silently

JSON allows numbers of any size, but JavaScript stores numbers as 64-bit floats and loses precision above 9,007,199,254,740,991. A database ID like 9007199254740993 parses without an error — as a different number. Send large IDs as strings.

6. Invisible characters

When the JSON looks perfect and still fails, suspect something you can't see:

  • A byte-order mark at the very start, added by some Windows editors. Save the file as “UTF-8” rather than “UTF-8 with BOM”, or in Python read it with encoding="utf-8-sig".
  • Smart quotes — “ and ” instead of " — from text pasted out of a word processor or chat app.
  • Real tabs or line breaks inside a string. Inside JSON strings they must be written as \t and \n.

Finding the error fast

  1. 1
    Paste the text into the JSON formatter. It shows the line and column of the first problem.
  2. 2
    Look at that spot and one character before it — the cause is usually just before where the parser gave up.
  3. 3
    Fix it and check again; there may be a second mistake further down.
  4. 4
    Once it's valid, format it with indentation so the next mistake is easier to spot.

If you have a working version and a broken one, format both with sorted keys and compare them with Text Compare to see exactly what changed.

Frequently asked questions

What does 'Unexpected token < in JSON at position 0' mean?

The text starts with <, so it's HTML, not JSON — usually an error page, a 404, or a login page returned by the server. Check the URL, the status code and whether you're still signed in.

Why are trailing commas not allowed in JSON?

JSON was defined as a small, strict subset of JavaScript object syntax in the early 2000s, before trailing commas were common, and the standard was deliberately frozen so every parser behaves the same.

Can JSON have comments?

No. Some tools accept a relaxed variant (JSONC or JSON5) that allows them, but standard parsers reject comments.

How do I fix 'Unexpected end of JSON input'?

The text you parsed was empty or cut off. Check that the request succeeded and returned a body, and that you're not parsing a partially downloaded or truncated file.

Is null valid JSON?

Yes. null, true, false, numbers, strings, arrays and objects are all valid. undefined, NaN and Infinity are not.

OnlineToolPro Editorial Team

Builds and tests the tools on this site

The team behind OnlineToolPro. We write guides from building and testing these tools, and check platform rules against official documentation such as YouTube Help. When something changes, we update the article and its date.

Other tools you might find useful next.