You paste a response into your app and get "Unexpected token". You edit a config file and the program refuses to start. JSON is simple, but it is strict, and one small slip is enough to break it. The good news is that a handful of mistakes cause nearly every error. Learn to spot these eight and most JSON problems take seconds to fix.

The fastest way to find the problem

  1. Open the JSON Validator.
  2. Paste your JSON into the box.
  3. Click Validate. A green message means the JSON is valid. A red message shows the error from the parser, which usually points to the position of the problem.
  4. Fix that spot, then validate again. Repeat until it turns green.
  5. Use Format to indent it neatly, or Minify to squash it into one line.

The check runs in your browser with the standard JSON parser, so nothing you paste is uploaded.

1. A trailing comma

This is the most common one. JSON does not allow a comma after the last item in an object or list.

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

Remove the final comma and it is valid. JavaScript tolerates it, which is why the mistake is so easy to make when you copy from code.

2. Single quotes instead of double quotes

{'name': 'Alice'}

Keys and text values must use double quotes: {"name": "Alice"}. Single quotes are not valid in JSON.

3. Keys without quotes

{name: "Alice"}

Every key must be wrapped in double quotes. This usually happens when someone pastes a JavaScript object into a place that expects real JSON.

4. Comments

JSON has no comments. Lines starting with // or blocks inside /* and */ will cause an error. Delete them, or move the notes somewhere else.

5. Missing or extra brackets

Every opening brace or bracket needs a matching closing one. If the error says the JSON ended unexpectedly, something was left open. Count your opening and closing braces and square brackets, and check the end of the text first.

6. A line break inside a text value

A real new line or tab inside a quoted string is not allowed. Write it as an escape instead, which means a backslash followed by the letter n for a new line, or by t for a tab.

7. Values that are not JSON

JavaScript has values that JSON does not. The word undefined is not valid, and neither are functions or NaN. JSON allows text, numbers, true, false, null, objects and lists, and nothing else. Replace the offending value with null or remove it.

8. Smart quotes and hidden characters

Curly quotation marks from a word processor or a chat app look right but are not the straight quote JSON needs. Text copied from PDFs and emails can also carry invisible characters. If the error points at something that looks fine, retype the quote marks around it.

Two problems the validator will not flag

  • Very large whole numbers. Numbers beyond about 15 digits can lose precision because they are read as JavaScript numbers. If an ID must stay exact, store it as a string.
  • Repeated keys. If an object repeats a key, only the last value survives, and no error is shown. If a value seems to be missing, check for a duplicate key.

JSON is not the same as a JavaScript object

This is the root of several of the mistakes above. JavaScript lets you use single quotes, trailing commas and unquoted keys. JSON allows none of them. When you copy an object from code and the validator complains, this difference is almost always the reason.

Format first, then read

A long single-line response is hard to read, and mistakes hide in it. Click Format and the JSON Formatter will lay it out with two spaces per level, which makes mismatched brackets much easier to see. Use Minify when you need the smallest version to send or store.

Got broken JSON?

The JSON Validator is free, shows the exact error and keeps your data on your device.

← All articles

More articles