seolivly
JSON troubleshooting

How to Fix Invalid JSON

JSON errors look cryptic but come from a short list of mistakes. Find the line, recognise the mistake, fix it in one edit.

Step 1: find where it breaks

Every JSON parser stops at the first character it cannot accept and reports a position. Paste the text into our JSON validator and press Validate: it converts that position into a line and column and puts your cursor on the spot. The mistake is usually right there or on the line just above it, because a missing comma only becomes a problem when the next item starts.

Step 2: match the mistake

Trailing comma

{"a": 1, "b": 2,} fails because strict JSON forbids a comma before a closing } or ]. JavaScript allows it, which is why it sneaks in when you copy from code. Delete the last comma.

Single quotes

{'name': 'Ana'} is a JavaScript object, not JSON. Strings and keys need double quotes: {"name": "Ana"}.

Unquoted keys

{name: "Ana"} also comes from JavaScript. Quote every key.

Missing comma between items

{"a": 1 "b": 2} reports an error at "b". Add the comma after 1.

Comments

JSON has no comments. Remove // and /* */ lines. If a config tool documents comment support (some editors accept "JSON with comments"), that is a separate format.

JavaScript values

undefined, NaN, Infinity and functions are not JSON. Use null, a number, or a string.

Unescaped characters inside strings

A raw line break or tab inside a string breaks parsing. Write \n and \t. A double quote inside a string must be escaped as \", and a backslash as \\. Windows paths are a classic trap: "C:\\Users\\ana".

Missing bracket at the end

"Unexpected end of JSON input" means the parser ran out of text while an object or array was still open. Either the file was cut off, or a closing } or ] is missing. Formatting the valid part often makes the gap obvious from the indentation.

Step 3: check what you are actually parsing

Some JSON errors are not in the JSON at all.

  • "Unexpected token <" usually means you received an HTML page, such as a 404 or login screen, where you expected JSON. Check the URL and the response status first.
  • An invisible character at position 0 often means a byte-order mark or stray whitespace at the start of the file. Re-save the file as UTF-8 without BOM.
  • Smart quotes such as “ and ” arrive when JSON is pasted through a word processor or chat app. Replace them with straight " quotes.

Preventing the next one

Generate JSON with code (JSON.stringify in JavaScript, json.dumps in Python) rather than writing it by hand whenever you can. Use an editor that highlights JSON syntax. And validate before you deploy: a broken config file is much cheaper to catch on your desk than in production.

Frequently asked questions

What does 'Unexpected token' mean in JSON?

The parser met a character it did not expect at that position, such as a quote, a bracket or a letter. The reported position points at the problem, or just after it.

What causes 'Unexpected end of JSON input'?

The text ends before every object and array is closed. Look for a missing closing bracket or a response that was cut off or empty.

Can JSON have trailing commas?

No. Strict JSON forbids a comma after the last item in an object or array.

Can I put comments in a JSON file?

Not in standard JSON. Some tools accept a variant with comments, but most parsers will reject them.

Is there a tool that fixes JSON automatically?

Some try, but automatic fixes can change your data. It is safer to let a validator point to the exact spot and make the one edit yourself.

Keep going