Unexpected token in JSON at position 0, JSON Parse Error, and Invalid JSON Format all mean that the parser did not receive complete, valid standard JSON.
The cause may be one missing comma, but it may also be an HTML login page, an empty response, or text that begins with a hidden BOM. This guide follows a practical sequence: understand the cause, locate the first failure, repair it, and verify the result with the ToolGarden online JSON Validator.
Why Do JSON Parse Errors Happen?
1. The response at position 0 is not JSON
Position 0 means the parser failed on the first character. An API expected to return JSON may instead return an HTML error page, login form, or proxy message beginning with <. Fix the URL, authentication, or server response before changing JSON fields.
A UTF-8 BOM can cause the same problem because the invisible character appears before the opening brace. An empty body more often produces Unexpected end of JSON input, which also points back to the data source.
2. Invalid quotes or property names
{
name: 'Tom',
"message": "He said "hello""
}Standard JSON is stricter than a JavaScript object literal. Property names and strings require double quotes, while quotes inside a string must be escaped. Single quotes, smart quotes, unquoted keys, and raw line breaks trigger a JSON Syntax Error.
3. Missing comma in JSON
{
"name": "Tom"
"age": 18
}When a comma is missing between object properties or array items, the parser usually fails near the next item. The reported position marks where parsing became impossible, while the missing comma is often at the end of the previous line.
4. A trailing comma
{
"name": "Tom",
"age": 18,
}Some JavaScript syntax accepts a trailing comma, but strict JSON does not. Remove the comma after the last property or item.
{
"name": "Tom",
"age": 18
}5. Truncated JSON or unclosed delimiters
An interrupted download, unfinished stream, or incomplete copy can end while a string, object, or array is still open. These cases often appear as Unexpected end of JSON input or JSON Decode Error. Inspect the final character and match every brace, bracket, and quote.
6. Parsing an object twice
JSON.parse expects a string. Passing an existing JavaScript object may coerce it to [object Object] and produce Unexpected token o. Check typeof first and only parse strings.
What Do Common JSON Errors Mean?
| Error message | Likely cause | Check first |
|---|---|---|
| Unexpected token in JSON at position 0 | The first character is HTML, a BOM, or plain text | Inspect the first 80 raw characters |
| Unexpected token < | The server returned HTML | Check status, redirects, and authentication |
| Unexpected token o | JSON.parse received an object | Check typeof input |
| Missing comma in JSON | Properties or items lack a separator | Inspect the field before the error |
| Invalid JSON Format | Quotes, commas, brackets, or values are invalid | Validate the earliest error |
| JSON Decode Error | Truncation, encoding, or incomplete syntax | Check encoding and the final character |
| JSON Syntax Error | The parser found an illegal token | Use line, column, or position |
How to Locate a JSON Error
- Preserve the raw text. Do not trim, replace characters, or swallow the original exception yet.
- Check the input type. JSON.parse should receive a string, not an object, undefined, or null.
- Inspect the first and final characters. JSON usually starts with { or [ and ends with the matching } or ].
- Inspect HTTP details. Check the status, Content-Type, redirects, and unmodified response body.
- Jump to the error offset. Position is a character offset, while line and column identify a row and column.
- Fix the first error first. Later failures may be a cascade from one missing quote or bracket.
const response = await fetch("/api/data");
const contentType = response.headers.get("content-type") ?? "";
const raw = await response.text();
console.log({
status: response.status,
contentType,
firstChars: raw.slice(0, 80)
});
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
if (!contentType.includes("application/json")) {
throw new Error("Expected JSON, received " + contentType);
}
const data = JSON.parse(raw.replace(/^\uFEFF/, ""));This code reads the raw text before validating the status and Content-Type. It then removes a possible BOM before parsing, which separates response failures from genuine JSON syntax failures.
The reported position often marks where the parser stopped rather than where the mistake started. A missing comma may be reported at the next property name even though the correction belongs at the end of the previous line.
How to Fix Invalid JSON Format
Restore strict JSON syntax
- Use double quotes for property names and strings.
- Add commas between adjacent properties and array items.
- Remove trailing commas from objects and arrays.
- Close every quote, brace, and bracket.
- Convert Python True, False, and None to true, false, and null.
- Remove comments, undefined, NaN, and Infinity from strict JSON.
- Escape quotes, backslashes, newlines, and control characters inside strings.
Fix HTML responses at the source
If the body begins with <!DOCTYPE or <html, do not replace characters to make it look like JSON. Correct the endpoint, authentication, proxy, or server exception, and return accurate status codes and Content-Type headers for both success and failure responses.
Do not concatenate JSON by hand
Use JSON.stringify when generating JSON dynamically. It escapes quotes, backslashes, and line breaks correctly and prevents many missing-comma and invalid-string bugs.
Validate structure after automatic repair
Automatic repair works well for trailing commas, comments, single quotes, and unquoted keys. It cannot reliably infer missing fields or the intended nesting of truncated data. Format the repaired result, inspect critical values, and use JSON Schema when the payload has business constraints.
Recommended Online JSON Validator: ToolGarden
When the failing character is unclear, paste the content into the ToolGarden online JSON Validator at toolgarden.xyz. It validates and formats the structure, which is faster than counting a position inside one minified line.
- Confirm whether the input is valid JSON.
- Format nested objects and arrays so commas and brackets are easy to inspect.
- Run JSON validation and formatting locally in the browser without uploading the input as a file.
- Continue to ToolGarden JSON Repair when the source uses non-standard JSON syntax.
Summary
For Unexpected token in JSON at position 0, inspect the raw response before editing fields. For Missing comma, Invalid JSON Format, or JSON Syntax Error, work backward from the earliest error and check quotes, commas, and brackets. Verify the repaired result once more with the ToolGarden online JSON Validator before returning it to your application.