How to Fix Common JSON Parse Errors

JSON.parse fails on trailing commas, single quotes, comments, bad escapes, and unclosed brackets. Read the first token, fix it, then validate before you format or minify.

Last updated: October 2, 2026

The problem

JavaScript throws SyntaxError: Unexpected token (or a close cousin) when JSON.parse cannot read the string. HTTP clients, WordPress wp_json_encode consumers, and Magento APIs fail the same way when the body is not JSON. The message names a token and sometimes a position. It does not rewrite the payload for you.

This solution is for common JSON parse errors you can fix in the text: commas, quotes, comments, escapes, and brackets. It is not for schema mistakes (wrong field types) after the string already parses.

Why parse fails

JSON is stricter than a JavaScript object literal. Keys and strings use double quotes. Trailing commas are illegal. Comments are illegal. 'Ada' is illegal. Concatenating two values without an array is illegal. A JSON validator will reject the same strings JSON.parse rejects, because this site uses that parser.

Copy-paste from application code is the usual source. A trailing comma that ESLint allows in a literal will break a .json file or an API body.

Fix the first token

  1. Paste the string into the JSON Validator. Do not format yet if you only need the error.
  2. Read the token and, when shown, the approximate line and column.
  3. If the token is , before } or ], delete the trailing comma.
  4. If the token is ', switch that string (and usually the key) to double quotes.
  5. If you see / from a comment, remove the comment.
  6. If a string shows a bad escape, use a legal JSON escape (\n, \u0041, not \x41).
  7. If braces or brackets do not pair, count them from the start of the value. Format only after validate succeeds, so you can see nesting.

Confirm with validate

Run validate after every change. The first fix often reveals a second problem later in the file. Keep going until the result is valid. The original text stays in the box so you are not comparing against a rewritten copy.

Then format or minify

When the result is valid and you need to read nested objects, use the JSON Formatter. When you need a compact body, minify only then. Minify will not succeed if validate would still fail.

Recurring mistakes

  • Editing JSON as if it were JavaScript
  • Pasting a PHP or Magento dump that includes extra output before the {
  • Smart quotes from a document editor
  • A BOM or concatenated JSON documents

If the error points at position 0, look for garbage before the first { or [, including a UTF-8 BOM or a PHP notice. Strip that, then validate again.

WordPress and Magento sites often emit HTML or a warning before JSON when a plugin prints during an Ajax response. The parse error then points at <. That is not a JSON syntax lesson; it is output-buffering. Fix the extra output, then treat whatever remains as JSON.

If two environments parse the same file differently, compare encodings and line endings before you rewrite commas. A UTF-16 download saved as UTF-8 looks like random tokens. Normalize encoding, then run the validator again. Only after the string is valid should you format it to hunt a logic bug.

On this page

Related solutions

Related articles

Need a tool for this ?

Open the free tools — no signup.

FAQs

newsletter signup

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Innovative Solutions For Modern Needs
Copyright © 2026 Yallasolve. all rights reserved.