Unterminated string in JSON: Fix JSON.parse() in JavaScript
JSON.parse() throws 'Unterminated string in JSON' when a string has no closing quote. Learn the causes in JavaScript and how to fix them.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →JavaScript throws "Unterminated string in JSON" when JSON.parse() reaches the end of the input, or a line break, before a string's closing double quote. The fix is to add the missing quote, or to repair text that was cut off or contains a raw newline.
What “Unterminated string in JSON” means
The error means the parser opened a string with a double quote and never found the closing one. RFC 8259 says a string starts and ends with a quotation mark and cannot contain raw control characters such as a line feed. So a missing quote, a raw newline or a truncated document all end the same way. V8 reports it as 'Unterminated string in JSON at position N'. The position points near where the parser gave up, which is often the end of the text, not the real mistake.
Causes and the fix for each
A closing quote is missing.
The string opens but never closes, so the parser runs into the next structure or the end of input.
Before (fails):{"name": "Ada}
After (valid):
{"name": "Ada"}
The text was truncated.
A network cut, a size limit or a token limit in an API response can stop the document in the middle of a string.
Before (fails):{"bio": "Writes compil
After (valid):
{"bio": "Writes compilers"}
A raw newline sits inside a string.
The standard does not allow unescaped line breaks in strings. They must be written as \n.
Before (fails):{"note": "line one
line two"}
After (valid):
{"note": "line one\nline two"}
A quote inside the value is not escaped.
The parser closes the string early, then fails on the leftovers, or reads a later quote as the start of a new string that never ends.
Before (fails):{"quote": "She said "hi}
After (valid):
{"quote": "She said \"hi\""}
A trailing backslash escapes the closing quote.
A backslash before the final quote turns it into a literal character, so the string stays open. This often happens with Windows paths.
Before (fails):{"path": "C:\temp\"}
After (valid):
{"path": "C:\\temp\\"}
How to fix it in JavaScript
- Read the position in the message and look at the text just before it. The broken string usually starts earlier than that spot.
- Check whether the input ends mid-string. Compare the length of the text you received with what the sender says it sent.
- Look for raw line breaks and tabs inside string values. Replace them with the escapes \n and \t.
- Escape any double quote inside a value as \" and any literal backslash as \\.
- Paste the text into the Validator to find the first bad character, or into the Repair tool to close the string and balance the brackets automatically.
- Fix the producer. Build the JSON with JSON.stringify() instead of string concatenation so quotes and newlines are escaped for you.
function safeParse(text) {
try {
return { ok: true, value: JSON.parse(text) };
} catch (err) {
if (err instanceof SyntaxError) {
const tail = text.slice(-40);
console.error('Bad JSON:', err.message, 'Ends with:', tail);
return { ok: false, error: err.message };
}
throw err;
}
}
const result = safeParse(rawBody);
if (!result.ok) {
// log, retry the request, or send the text to a repair step
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Unterminated string in JSON at position 12 | V8 (Chrome, Node.js) found no closing quote. | Close the string or escape the inner characters. |
| Bad control character in string literal in JSON at position 20 | A raw newline or tab is inside a string. | Escape it as \n or \t. |
| JSON.parse: unterminated string at line 1 column 15 of the JSON data | Firefox wording for the same problem. | Add the missing closing quote. |
| Unexpected end of JSON input | The text stops before the document is complete. | Check for truncation and get the full response. |
| Unexpected token } in JSON at position 18 | An unescaped quote closed a string early. | Escape the inner quote as \". |
Related errors
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
- Invalid JSON Meaning: What It Is and How to Fix It
- Fix JSON SyntaxError: Unexpected Token
- JSON Missing Bracket Error: How to Find and Fix It
- [Fix JSON Single Quote Errors Instantly — Free Online Tool [2026]](/blog/fix-json-single-quotes)
Key takeaways
- The error means a string was opened and never closed.
- Raw newlines inside strings cause it, because JSON requires \n.
- Truncated responses are a common source, so compare lengths.
- Build JSON with JSON.stringify() rather than string concatenation.
- Use the Validator to locate the problem and the Repair tool to fix it.
FAQ
Why does the position in the error point to the end of my text?
The parser keeps reading inside the open string until the input ends. It reports where it stopped, not where the string began. Look backward for the first unclosed quote.
Can JSON strings span several lines?
No. RFC 8259 does not allow raw line breaks inside a string. Use the \n escape instead.
Why do I get this error only on some API responses?
Long responses are more likely to be cut off by a timeout, a size limit or a token limit. The same code works on short ones.
Does JSON.stringify() prevent this error?
It escapes quotes, backslashes and control characters in strings, so its output does not contain unterminated strings. The problem comes from hand-built strings or truncated data.
Can I fix a truncated document automatically?
A repair tool can close the open string and add missing brackets. The data that was cut off stays lost, so check the result.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →Related Articles