Free & private — your files never leave your device.Compress a photo to 100 KB →
Back to Blog

JSON “Unexpected Token” Errors: Why They Happen and How to Fix Them

You paste some JSON into your code, run it, and get hit with “Unexpected token” or “Unexpected end of JSON input.” The message points at a character position that means nothing to you, and the file looks fine.

It almost never is fine. JSON is strict, and it fails on tiny details that look perfectly reasonable to a human. Here are the usual culprits.

1. A trailing comma

This is the number one offender. In JavaScript you can leave a comma after the last item, but JSON does not allow it.

{ "name": "Ana", "age": 30, }   // invalid
{ "name": "Ana", "age": 30 }    // valid

2. Single quotes instead of double quotes

JSON only accepts double quotes, for both keys and string values. {'name': 'Ana'} is valid JavaScript but not valid JSON.

3. Keys without quotes

{name: "Ana"} works as a JavaScript object, but in JSON every key must be a quoted string.

4. Comments

JSON has no comments. A // or /* */ will break the parser. If you need notes, put them in a separate field or strip them before parsing.

5. Values JSON doesn’t know

undefined, NaN, Infinity and functions are not valid JSON values. Use null, a string, or leave the field out.

6. Unescaped quotes and line breaks inside strings

If a string contains a double quote, it must be written as \". A raw line break inside a string is also invalid; use \n instead. Text copied from a document is a frequent source of this.

7. Invisible characters

A byte order mark at the start of a file, or “smart quotes” pasted from a word processor, look normal on screen but confuse parsers. If everything else looks right, this is worth checking.

8. “Unexpected end of JSON input”

That one usually means the data is cut off, a bracket is missing, or an API returned an empty body. Check that every { has a matching } and every [ has a matching ].

The 10-second way to find the problem

Paste your JSON into the JSON formatter and validator. It uses your browser’s own parser, so you get the real error message, and the formatted output makes mismatched brackets obvious because the indentation goes wrong right where the mistake is.

Because it runs locally, you can safely paste API responses that include real data. Nothing leaves your machine.

Need the data in a spreadsheet?

Once your JSON is valid, the JSON to CSV converter turns an array of objects into rows and columns, and the CSV to JSON converter goes the other way.

Next time you see “Unexpected token,” don’t squint at the whole file. Check for a trailing comma and single quotes first. Nine times out of ten, that’s it.