Common JSON Errors and How to Fix Them (2026)
This is a reference guide you can bookmark. Every common JSON.parse error message is listed with its exact cause, an invalid vs valid code example, and the fix. Use it when you hit a JSON syntax error and need to understand it fast.

Table of Contents
Why JSON errors matter and how to read them
JSON.parse() throws a SyntaxError when it encounters invalid JSON. The error message always contains two pieces of information: a description of what went wrong, and the position (character offset from the start of the string) where the parser failed.
For example: SyntaxError: Unexpected token ',', ..."age": 30,}" is not valid JSON (Chrome) or SyntaxError: JSON.parse: unexpected character at line 3 column 14 (Firefox).
The error message wording varies between browsers and JavaScript engines. Chrome, Firefox, Node.js, and Deno each have their own phrasing. The underlying cause is always the same. This guide covers the cause and fix for every common error, regardless of which browser or runtime produced the message.
The fastest way to find and fix an error: paste the JSON into the SammaPix JSON Formatter. It runs JSON.parse locally and shows the error message with the character position, letting you jump directly to the problem.
Unexpected token
Error messages:
Unexpected token 'X', ..."..." is not valid JSON(Chrome)JSON Parse error: Expected 'X'(Safari)Unexpected token X in JSON at position N(Node.js older)
Cause: The parser encountered a character it did not expect at a specific position. The character X in the message is the unexpected character. This is a generic error that covers many specific cases (trailing comma, single quote, unquoted key, comment). The specific character tells you which sub-case you are dealing with:
| Unexpected character | Likely cause |
|---|---|
| ' | Single quote used instead of double quote for a key or string value |
| } or ] | Trailing comma before the closing bracket: {"a":1,} |
| / | Comment in JSON (// or /*) |
| letter (a-z) | Unquoted key, or an invalid literal like undefined, NaN |
| second " | Missing comma between two adjacent string values or objects |
Unexpected end of JSON input
Error messages:
Unexpected end of JSON inputJSON.parse: unexpected end of data at line N column N(Firefox)SyntaxError: Unterminated string in JSON at position N
Cause: The JSON string ended before the parser finished reading a complete value. The most common causes:
- Missing closing bracket. An object or array was opened but never closed.
- Truncated response. The API response was cut off mid-stream due to a network error, timeout, or server crash. The JSON is incomplete.
- Empty string.
JSON.parse("")throws this error because an empty string is not valid JSON. - Unterminated string value. A string was opened with a double quote but the closing quote is missing.
// Invalid: missing closing brace
{"name": "Alice", "scores": [10, 20, 30]
// Valid
{"name": "Alice", "scores": [10, 20, 30]}
// Invalid: truncated string value
{"name": "Ali
// Valid
{"name": "Alice"}Fix: Add the missing closing bracket. If the JSON comes from an API, check that the full response body was received before parsing. Use response.text() to inspect the raw body before calling response.json(). Guard against empty strings: text ? JSON.parse(text) : null.
Paste broken JSON, see the exact error and position
The JSON Formatter runs JSON.parse in your browser and shows the error with character position. Fix the error, format, copy. No upload, no server.
Open JSON Formatter, FreeTrailing comma
Error message: Unexpected token '}' or Unexpected token ']'
Cause: A comma appears after the last item in an object or array. The parser expects a new item after the comma but finds a closing bracket instead.
// Invalid: trailing comma in object
{
"name": "Alice",
"age": 30,
}
// Invalid: trailing comma in array
[1, 2, 3,]
// Valid object
{
"name": "Alice",
"age": 30
}
// Valid array
[1, 2, 3]Fix: Remove the comma after the last item. In a multi-line JSON object, this is the comma on the last property line before the closing }.
Why this happens so often: JavaScript (ES5+) allows trailing commas in object and array literals, and many code style guides actively encourage them (because they make diffs cleaner). When developers copy a JS object literal into a JSON context, the trailing comma follows. The fix is always to remove the comma.
Single quotes instead of double quotes
Error message: Unexpected token '''
Cause: Single quotes are used for a key name or a string value. JSON only accepts double quotes.
// Invalid: single-quoted key and value
{'name': 'Alice'}
// Invalid: single-quoted value only (key is double-quoted)
{"name": 'Alice'}
// Valid
{"name": "Alice"}Fix: Replace all single quotes with double quotes. In most code editors, a find-and-replace on ' will work, but be careful: if any of your string values legitimately contain apostrophes, those will need to be escaped as \' inside a double-quoted string (though in practice they do not need escaping in JSON since JSON strings are delimited by double quotes; apostrophes are literal characters inside a double-quoted JSON string).
Unquoted keys
Error message: Unexpected token 'n' (or whatever the first letter of the unquoted key is)
Cause: A key is written without quotes, as in a JavaScript object literal. JSON requires all keys to be double-quoted strings.
// Invalid: unquoted keys
{name: "Alice", age: 30}
// Valid
{"name": "Alice", "age": 30}Fix: Add double quotes around every key. The parser sees the first letter of the unquoted key and does not recognize it as a valid start of a JSON value (which would need to be a ", {, [, t for true, f for false, n for null, or a digit).
Comments in JSON
Error message: Unexpected token '/'
Cause: A // or /* */ comment appears in the JSON. Comments are not part of the JSON specification.
// Invalid: line comment
{
"host": "localhost", // database host
"port": 5432
}
// Invalid: block comment
{
/* database config */
"host": "localhost"
}
// Valid: no comments
{
"host": "localhost",
"port": 5432
}Fix: Remove all comments. If you need comments in a configuration file, rename the file to use the .jsonc extension (JSON with Comments) and use a parser that supports it. VS Code reads settings.json as JSONC. Node.js does not support JSONC natively; use a library like jsonc-parser.
undefined, NaN, and Infinity values
Error messages:
Unexpected token 'u'(for undefined)Unexpected token 'N'(for NaN)Unexpected token 'I'(for Infinity)
Cause: These JavaScript values are not part of the JSON specification and cannot be represented in JSON.
// Invalid
{"score": NaN, "max": Infinity, "result": undefined}
// Valid: use null for missing or non-representable values
{"score": null, "max": null, "result": null}Fix: Replace these values with null, or use a number that represents the concept (for example, -1 or a very large number instead of Infinity). Note: when you call JSON.stringify on a JavaScript object with these values, NaN and Infinity are serialized as null, and undefined properties are dropped entirely. The raw literals appear in JSON only if you manually write them as text.
Missing comma between items
Error messages: Unexpected string, Unexpected token '"'
Cause: Two properties or array elements are adjacent without a comma between them. The parser expects a comma or a closing bracket after a value, but finds the start of a new value instead.
// Invalid: missing comma between properties
{
"name": "Alice"
"age": 30
}
// Invalid: missing comma in array
[1 2 3]
// Valid
{
"name": "Alice",
"age": 30
}
// Valid
[1, 2, 3]Fix: Add the missing comma after the preceding value. When you see Unexpected token "" (a double quote), the parser reached the start of a new key while expecting a comma or closing bracket.
Duplicate keys
Error message: None from JSON.parse (see explanation below)
Cause: An object has two properties with the same key. The JSON specification (RFC 8259) says duplicate keys make an object "semantically invalid" but does not require parsers to throw an error. Most JSON parsers (including JSON.parse) silently accept duplicate keys and use the last value.
// Technically invalid but parsed without error by most parsers
{
"name": "Alice",
"name": "Bob"
}
// JSON.parse result: {"name": "Bob"} (last value wins)
// Valid: use distinct keys
{
"firstName": "Alice",
"lastName": "Bob"
}Why this matters: Because JSON.parse does not throw, duplicate keys are silent bugs. The value you see in your application may not be the value you intended, and the behavior varies between parsers (some use the first value, some use the last). A JSON linter or formatter that checks for duplicates will catch this when JSON.parse does not.
Mismatched or missing brackets
Error messages: Unexpected token ']', Unexpected token '}', or Unexpected end of JSON input
Cause: A closing bracket ] is used to close an object {, or a } is used to close an array [. Or brackets are simply missing or extra.
// Invalid: wrong closing bracket type
{"scores": [10, 20, 30}]
// Invalid: extra closing bracket
{"name": "Alice"}}
// Valid
{"scores": [10, 20, 30]}Fix: Match every { with a } and every [ with a ]. For complex nested JSON, a formatter with syntax highlighting makes bracket matching visible. An editor with bracket-pair colorization (VS Code has this built-in) helps trace deep nesting.
Unescaped control characters in strings
Error message: Invalid control character at position N or Unexpected token
Cause: A raw control character (tab, newline, carriage return, or other characters with Unicode code points 0x00 to 0x1F) appears inside a JSON string without being escaped. JSON strings must have these characters escaped as \t, \n, \r, or \uXXXX.
// Invalid: raw newline inside string value
{"message": "Line one
Line two"}
// Valid: escaped newline
{"message": "Line one\nLine two"}
// Also valid for tab
{"path": "C:\\Users\\Alice\\file.txt"}Fix: Escape control characters. A raw literal newline inside a JSON string must be written as \n. This error often appears when JSON is built by string concatenation or template literals instead of JSON.stringify. Always use JSON.stringify to produce JSON from JavaScript values.
Validate JSON and see the error location instantly
Paste JSON. See error with position. Fix it. Format. No upload, no account, runs entirely in your browser.
Quick-fix workflow for any JSON error
Use this sequence for any JSON parse error, regardless of where it originates:
- Read the error message character. The character X in "Unexpected token X" tells you the specific error type. Use the table in the Unexpected token section above to identify it.
- Note the position. The position N (character offset from zero) tells you exactly where the error is. Count from the start of the JSON string, or use a formatter that highlights the position.
- Paste into a formatter. Open sammapix.com/tools/json-formatter. Paste the JSON. The error message and character position are shown immediately. Jump to that position.
- Apply the fix. Find the error from this guide. Make the correction in the formatter's input.
- Format again. Click Format. If there are multiple errors (common in manually written JSON), the formatter will report the next error after you fix the first one. Repeat until the JSON formats cleanly.
- Fix the root cause. If the JSON came from a server, a build script, or a string concatenation, fix the generator so it produces valid JSON from the start. The best fix is always to use
JSON.stringifyrather than manually building JSON strings.
| Error | Typical message | Fix |
|---|---|---|
| Trailing comma | Unexpected token '}' | Remove comma after last item |
| Single quotes | Unexpected token ''' | Replace all single quotes with double quotes |
| Unquoted key | Unexpected token 'n' (first letter) | Add double quotes around every key |
| Comment | Unexpected token '/' | Remove the comment. Use .jsonc if comments are needed |
| undefined / NaN | Unexpected token 'u' / 'N' | Replace with null or a valid number |
| Missing comma | Unexpected string / Unexpected token '"' | Add comma after the preceding value |
| Truncated JSON | Unexpected end of JSON input | Check full response received. Add missing closing bracket |
| Control character | Invalid control character | Escape as \n, \t, \r, or \uXXXX. Use JSON.stringify instead of string concat |
Related tools
- JSON Formatter: format, prettify, minify, and validate JSON in your browser. Shows error message and character position for every JSON parse error. No upload, no account. See also: JSON formatting complete guide.
- URL Encode / Decode: decode percent-encoded strings, which frequently appear when JSON is transmitted as a URL query parameter. See URL encoding guide.
- Hash Generator: generate checksums from text or files. Useful for verifying JSON file integrity. See Hash Generator guide.
- Image to Base64: encode images as Base64 strings for embedding in JSON payloads without a file upload.
Developer tools that run entirely in your browser
Format JSON, encode URLs, hash files, convert images. No upload, no server, no account.
FAQ
What does 'Unexpected token' mean in a JSON parse error?
The error 'Unexpected token X at position N' means the JSON parser encountered a character it did not expect at a specific location. Common causes include: a single quote instead of a double quote (Unexpected token ' at position 0), an unquoted key (Unexpected token n for a key starting with 'n'), a trailing comma before a closing brace (Unexpected token }), or a comment starting with // (Unexpected token / at position N). The position number tells you the character offset from the start of the string where the parser failed. Count from zero to find the exact character.
What causes 'Unexpected end of JSON input'?
This error means the JSON string ended before the parser finished reading a complete value. Common causes: a missing closing brace (} or ]), a truncated string (the JSON was cut off in the middle of a value or key), or an empty string passed to JSON.parse. The fix is to check whether the full JSON was received. In API contexts, this often means the response was truncated due to a network error, a Content-Length mismatch, or a server-side error that terminated the response body early.
Can trailing commas appear in JSON arrays?
No. Trailing commas are not allowed in JSON arrays or objects. [1, 2, 3,] is invalid JSON. [1, 2, 3] is valid. This is a common mistake when copying array literals from JavaScript into a JSON context, because JavaScript (ES5+) permits trailing commas in both array and object literals. The JSON specification (RFC 8259) explicitly forbids them. Remove the trailing comma to fix the error.
How do I quickly find and fix a JSON error?
The fastest workflow: paste your JSON into the SammaPix JSON Formatter (sammapix.com/tools/json-formatter). It runs JSON.parse in your browser and reports the error message and character position. Jump to that position, identify the error type (trailing comma, missing quote, single quote, etc.), fix it, and format again. For large JSON strings, the position number is critical: count from zero or use a text editor that shows character offset in the status bar. VS Code shows line and column in the bottom-right corner.
Why does my JSON work in JavaScript but fail in JSON.parse?
JavaScript object literals and JSON are not the same format. JavaScript allows unquoted keys, single-quoted strings, trailing commas, comments, and values like undefined, NaN, and Infinity. JSON allows none of these. If you wrote an object literal in JavaScript code and then tried to serialize it as JSON, the output of JSON.stringify is valid JSON, but the raw literal text is not. Always use JSON.stringify to convert JavaScript objects to JSON strings, never try to use a raw JS object literal as a JSON string.