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.
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.
, before } or ], delete the trailing comma.', switch that string (and usually the key) to double quotes./ from a comment, remove the comment.\n, \u0041, not \x41).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.
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.
{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.
The string is not valid JSON. Read the parse error, fix that token, validate, then minify.
You often passed a JavaScript object into JSON.parse, which stringifies to “[object Object]”. Parse a string, not an object.
Not in standard JSON. Use a sidecar file or a format that allows comments. This validator will reject comments.
Format will not rewrite invalid JSON on this site. Validate, fix, then format if you need indentation.