Skip to content

Guide

How to fix JSON errors (Unexpected token)

When a program only says "invalid JSON", here is how to read the error message, find the exact spot, and fix the most common causes.

4 min read

On this page
  1. 1Step 1: Read the position in the error message
  2. 2Step 2: The ten most common causes
  3. 3Step 3: Check the fix
  4. 4Habits that prevent JSON errors

You edit a config file and the program will not start, or you call an API and get nothing but Unexpected token. JSON is strict: one stray comma or quote and the whole file cannot be read. The good news is that the causes are nearly always the same handful, so once you find where it breaks, the fix is quick.

Step 1: Read the position in the error message

Error messages look different in every program, but most include where the problem is.

Where you see it Example How to read the position
Chrome, Edge, Node.js Expected double-quoted property name in JSON at position 7 (line 1 column 8) line is the line, column the character on that line
Chrome (older versions) Unexpected token } in JSON at position 42 position counts characters from the start, from 0
Firefox JSON.parse: expected ',' or '}' after property value at line 3 column 5 line 3, character 5
Python json.decoder.JSONDecodeError: Expecting ',' delimiter: line 3 column 5 (char 20) line 3, character 5; char counts from the start

The hard case is a bare position 42 in JSON that sits on one long line. Paste the whole thing into the JSON formatter: it tells you the line and character and what is wrong in plain words, and “Show it in the input” puts the cursor right there.

The position marks where the text stops making sense. For a missing comma that is the start of the next item, so check the end of the line just before it too.

Step 2: The ten most common causes

1. A comma after the last item

{ "name": "Kim", "age": 30, }

Easy to leave behind after deleting or reordering items. JavaScript and Python code allow it; JSON does not. Delete the comma right before } or ]. This is the most common cause of all.

2. A missing comma

{ "name": "Kim"
  "age": 30 }

A new line was added without a comma at the end of the line above. The error points at "age", but the fix belongs at the end of the previous line.

3. Single quotes

{ 'name': 'Kim' }

JSON uses double quotes only. This is what you get when you copy a Python dictionary printed with print(). In Python, use json.dumps(data, ensure_ascii=False) instead.

4. Curly quotes (“ ”)

{ “name”: “Kim” }

The hardest to spot, because they look almost the same. Word, Pages, phone notes apps and many chat apps turn straight quotes into curly ones. Edit JSON only in a plain text or code editor, and send it as a file rather than in a message.

5. Keys without quotes

{ name: "Kim" }

Fine in a JavaScript object, not in JSON: every key needs double quotes, as in "name".

6. Comments

{ "port": 8080 // for development }

JSON has no comments. Some files accept them (JSONC, such as VS Code settings), but the same text fails anywhere else. If you need a note, store it as a value: "_comment": "for development".

7. Backslashes in Windows paths

{ "path": "C:\Users\me\file.txt" }

In JSON a backslash starts a special character such as \n (new line). Write each backslash twice, "C:\\Users\\me\\file.txt", or use forward slashes, "C:/Users/me/file.txt", which most programs accept.

8. Values like True and None

{ "ok": True, "memo": None }

JSON only knows lower-case true, false and null. Python’s True and None, and JavaScript’s undefined and NaN, are not JSON.

9. Invisible special spaces

JSON copied from a web page, PDF or chat sometimes carries a no-break space or a full-width space instead of a normal one. You cannot see the difference, so if the error points at what looks like nothing, suspect this. Delete the space and type it again. The JSON formatter reports these spaces separately.

10. HTML instead of JSON

Unexpected token '<', "<!DOCTYPE "... is not valid JSON

The classic API error. The JSON is not broken: the server sent a web page instead of JSON, usually a 404 page because the address is wrong, a login page because the session expired, or a server error page. Paste what you received and you will see it starts with <html>. Check the address, your sign-in and the server.

Likewise, Unexpected end of JSON input, or Python’s Expecting value: line 1 column 1 (char 0), usually means the response was empty.

Step 3: Check the fix

  1. Paste the corrected JSON into the JSON formatter again and make sure it says “Valid JSON”. With several mistakes, each fix reveals the next one.
  2. Keep it pretty-printed: you can see where each bracket opens and closes, which prevents the next mistake.
  3. Format the original and the fixed version the same way and put them into Compare text by line to confirm exactly what you changed.

Habits that prevent JSON errors

  • Edit JSON only in a text or code editor: word processors and notes apps change quotes and spaces.
  • Send JSON as a file: pasting it into a chat can change quotes and spaces or cut long lines.
  • Do not type table data by hand: to move a spreadsheet into JSON, use XLSX to JSON and the commas and quotes will be right. To view JSON as a table, use JSON to XLSX.
  • Be careful where you paste JSON with API keys: some online validators send what you paste to their server. The JSON formatter here checks it inside your browser and nothing leaves your device.

Tools used in this guide

Related guides