DEV Community

pulkitgovrani
pulkitgovrani Subscriber

Posted on Originally published at ilovekit.app on

How to Fix "Unexpected Token" and Other Invalid JSON Errors

Errors such as "Unexpected token } in JSON at position 42" or "Expected property name or '}'" almost always come from a small syntax slip. JSON is stricter than JavaScript object syntax, and it is easy to write something that looks fine but is not valid. These are the mistakes behind the vast majority of parse errors.

1. Trailing commas

JSON does not allow a comma after the last item in an object or array. This is the single most common error, especially after deleting a line from the end of a list.

{ "a": 1, "b": 2, }    // invalid
{ "a": 1, "b": 2 }     // valid

[1, 2, 3, ]              // invalid
[1, 2, 3]                // valid
Enter fullscreen mode Exit fullscreen mode

2. Single quotes

Strings and keys must use double quotes. { 'name': 'Ada' } is valid JavaScript but invalid JSON. Replace every single quote used as a delimiter with a double quote, and escape any double quote inside a string as \".

3. Unquoted keys

In JavaScript you can write { name: "Ada" }. In JSON every key must be a double-quoted string: { "name": "Ada" }.

4. Comments

Standard JSON has no comments. Neither // nor /* */ is allowed. If you need comments, use a superset such as JSONC (used by editors like VS Code) or a format like YAML, or strip the comments before parsing.

5. Values JSON does not support

  • undefined, NaN, and Infinity are not valid JSON values. Use null or a string instead.
  • Numbers cannot have leading zeros (007) or a leading plus sign, and hex like 0xFF is not allowed.
  • Strings cannot contain raw newlines or tabs; escape them as \n and \t.
  • Dates have no native type; store them as ISO 8601 strings.

6. Invisible and structural problems

  • A byte-order mark (BOM) at the start of a file can trigger "unexpected token" at position 0.
  • Two objects back to back ({...}{...}) instead of a single object or an array.
  • A truncated response: the payload was cut off, so a closing brace or bracket is missing.
  • The response is actually HTML (an error page or a login redirect) rather than JSON, which shows up as "Unexpected token <".

How to find the problem fast

  1. Copy the exact text that is being parsed, not what you think it contains.
  2. Paste it into a validator; it should point to the line and column of the first error.
  3. Look at the character just before the reported position. The real mistake is usually right there, such as a missing comma or an extra one.
  4. Fix one error at a time and re-validate; later errors are often hidden behind the first.
  5. If the error is at position 0 and you see "<", log the raw response body and check the HTTP status.

Keep real data private

API responses often contain tokens, emails, and other personal data. Validate them with a tool that runs in your browser, so the payload isn't uploaded to someone else's server.

Frequently asked questions

Why does JSON.parse say "Unexpected token < in JSON at position 0"?

The text starts with a "<", which almost always means you received an HTML page (a 404, 500, or login page) instead of JSON. Check the HTTP status and log the raw response.

Can JSON have comments or trailing commas?

Not in standard JSON. Comments and trailing commas are allowed in supersets such as JSONC or JSON5, but a strict parser will reject them.

Are single quotes valid in JSON?

No. Keys and string values must use double quotes.

How do I validate JSON without uploading it?

Use a validator that runs client-side in your browser and confirm in the Network tab that no request is made when you paste your data.

Try it: JSON Formatter & Validator — free, runs in your browser, nothing is uploaded.

Originally published at ilovekit.app.

Top comments (0)