HassleFreeTools

Common JSON Syntax Errors and How to Fix Each One

· 6 min read

Your code stops with "Unexpected token" or "Unexpected end of JSON input", or an app refuses a settings file you edited by hand. JSON is strict, and one stray comma is enough to break the whole file. This guide shows how to find the exact spot, what the usual error messages mean, and ten common mistakes that cause them.

Find the error in four steps

  1. Open the JSON Formatter and Validator and paste your JSON into the JSON input box, or use Open file for a .json file of up to 20 MB.
  2. Read the line under the box. The tool checks the JSON as you type (very large inputs are checked when you press Format) and names the first problem with its line and column.
  3. Press Format. The faulty character is selected in the input, and the result side shows an excerpt with a marker under it.
  4. Fix that one problem and check again. Only the first error is reported, so a file with three mistakes takes three rounds.

Once the JSON is valid, Format lays it out with 2 spaces, 4 spaces or tabs, and Copy or Download gives you the result. Everything runs in your browser, so API responses with private data are not uploaded.

What the error messages from your code mean

Programs describe JSON errors in their own words, and the wording changes between versions. These are some you are likely to meet:

Message (examples) Usual cause
Unexpected token (an angle bracket) … is not valid JSON You received an HTML page, not JSON
Unexpected end of JSON input The text is empty or was cut off
Expected double-quoted property name A trailing comma before } or a name without double quotes
Expecting property name enclosed in double quotes (Python) Single quotes or a name without quotes; in some versions, a trailing comma
Expecting value: line 1 column 1 (Python) Empty text, or text that is not JSON at all

The first one deserves a closer look. When a request fails, many servers send back an error page written in HTML, which starts with the angle bracket of a tag or a DOCTYPE line. Your code then tries to read that page as JSON. The JSON itself is not the problem: check the address you called, the login and the response status instead.

Ten common mistakes and their fixes

1. A trailing comma

JavaScript and many config formats allow a comma after the last item. JSON does not.

Broken:

{
  "name": "Asha",
  "city": "Pune",
}

Fixed: delete the comma after "Pune". The same rule applies before the ] that closes a list.

2. A missing comma

This usually happens when you add a line in the middle of an object:

{
  "name": "Asha"
  "city": "Pune"
}

The tool's message then says that a comma is probably missing. Note that it points at "city", the value after the gap, because that is where the parser noticed something was wrong.

3. Single quotes or curly quotes

JSON accepts only straight double quotes around names and text. {'name': 'Asha'} is fine in Python and JavaScript, but not in JSON. Curly quotes (“ ”) are a quieter form of the same problem: word processors and some phone keyboards turn straight quotes into curly ones as you type, so JSON pasted from a document or a chat often contains them. Fixed: {"name": "Asha"}.

4. Property names without quotes

{name: "Asha"} is an ordinary JavaScript object, which is why it looks right to many developers. In JSON every name needs double quotes: {"name": "Asha"}.

5. Comments

JSON has no comments, so // and /* */ are errors. If a note has to travel with the data, store it as a normal field, such as "note": "prices include GST".

6. Values that JSON does not have

JSON knows strings, numbers, true, false, null, objects and arrays, and nothing else. JavaScript's undefined, NaN and Infinity are errors; use null or a string instead. So are True, False and None, which appear when Python data is printed instead of converted:

import json

data = {"name": "Asha", "active": True, "manager": None}
print(str(data))         # {'name': 'Asha', 'active': True, 'manager': None}  (not JSON)
print(json.dumps(data))  # {"name": "Asha", "active": true, "manager": null}

Create JSON with your language's JSON function (json.dumps in Python, JSON.stringify in JavaScript) rather than by printing data or joining strings together.

7. Numbers with leading zeros

"phone": 09876543210 is invalid, because a JSON number cannot have more digits after a leading 0 (0.5 is fine, 05 is not). Phone numbers, PIN codes and IDs are not amounts anyway, so write them as text: "phone": "09876543210".

8. Brackets or strings that never close

"Unexpected end of JSON input" means something was opened and never closed, or there was no text at all. Common causes are copying only part of a long log line and a response that was cut off. A string cannot run over two lines either; write a line break inside text as \n:

{"address": "12 MG Road\nPune 411001"}

9. Invisible characters

Text copied from a web page, a PDF or a chat can carry non-breaking spaces or other invisible characters between values. The JSON looks perfect and still fails. The formatter reports such a character with its position and selects it when you press Format, so you can delete it and type an ordinary space.

10. More than one value at the top level

A JSON document holds exactly one value. Two objects pasted one after the other, or a JSON Lines (.jsonl) file with one object per line, fail right after the first object. Put the values in a list, separated by commas:

[
  {"id": 1, "status": "paid"},
  {"id": 2, "status": "pending"}
]

Valid, but still risky

Two things pass the check and can still cause bugs, so the tool warns about them:

  • Repeated keys. {"id": 1, "id": 2} is valid JSON, but most programs keep only the last value. The tool keeps both and warns you, so you can decide which one is right.
  • Very large whole numbers. JavaScript holds whole numbers exactly only up to 9,007,199,254,740,991. The formatter keeps your digits as written, but the program that reads the file may round a 19-digit order ID. Store such IDs as strings.

Keep in mind that the tool checks syntax only. A file can be perfectly valid JSON and still be wrong for your app, for example "age": "25" where the app expects a number.

Habits that prevent JSON errors

  • Generate JSON, do not type it. Let your code or your API client write it, and edit it in a code editor rather than a word processor.
  • Check the line before the reported position. Missing commas and unclosed quotes often show up one line later than the actual mistake.
  • Keep identifiers as text. Anything with leading zeros or more than 15 digits is safer in quotes.

Wrapping up

The fixes above come down to a few rules: double quotes everywhere, no trailing commas, no comments, and only true, false and null as bare words. Paste the file into the JSON Formatter, fix the first error it shows, and repeat until none are left. If you then need the data in a spreadsheet, JSON to CSV turns an array of records into rows. More tools for this kind of work are in the developer tools section.