← All articles

Unterminated String in JSON: Causes and Fixes

An unterminated string in JSON means an opening quote has no closing quote — usually an unescaped quote, a raw newline, or truncated data. Broken and fixed examples.

SyntaxError: Unterminated string in JSON at position N means the parser started reading a string (saw an opening ") and reached the end of the input — or a line break — before finding the closing ". The string was never closed. Here's why that happens and how to fix it.

Which string-error am I getting?

What the Error Looks Like

// V8 (Chrome / Node / Edge)
SyntaxError: Unterminated string in JSON at position 27

// Firefox
SyntaxError: JSON.parse: unterminated string at line 2 column 10 of the JSON data

// Safari
SyntaxError: JSON Parse error: Unterminated string

The position points at (or just after) the opening quote of the string that never closed — work forward from there.

Why It Happens

Cause 1 — An unescaped quote ends the string early

A literal " inside a value closes the string prematurely; the rest of the line is then read as unexpected tokens, and the real string is left "open."

// ❌ broken — the quote before hello closes "He said "
{ "note": "He said "hello" to her" }

// ✅ fixed — escape inner quotes with backslash
{ "note": "He said \"hello\" to her" }

Cause 2 — A raw line break inside the string

JSON strings can't span raw newlines. A literal line break terminates the string and leaves it unclosed.

// ❌ broken — real newline inside the string
{ "bio": "line one
line two" }

// ✅ fixed — use the \n escape, no raw newline
{ "bio": "line one\nline two" }

Cause 3 — Truncated data

A response cut off mid-string (dropped connection, buffer/size limit, partial read) leaves a string open at the end of the input.

// what arrived (connection dropped):
{"user":{"name":"Ada Lovel
// → Unterminated string

How to Fix It — Step by Step

  1. Go to the reported position and find the opening " that has no matching close.
  2. Look for an unescaped " inside the value — escape it as \".
  3. Look for a raw newline/tab in the value — replace it with \n / \t.
  4. Check for truncation — compare the byte length to Content-Length; if short, the data was cut off (a code fix, not a text fix).
  5. Stop hand-building JSON. Use JSON.stringify(), which escapes quotes and control characters for you.
// Prevention: never concatenate strings into JSON
const json = JSON.stringify({ note: userInput }); // always valid

Frequently Asked Questions

What causes "Unterminated string in JSON"?

An opening " with no matching closing " — usually an unescaped quote inside the value, a raw line break, or a response that was truncated mid-string.

How do I include a quote or newline inside a JSON string?

Escape them: \" for a double quote, \n for a newline, \t for a tab. Building the JSON with JSON.stringify() does this automatically.

Is this the same as "Unexpected end of JSON input"?

Related but distinct. "Unterminated string" means a string is open; "Unexpected end of JSON input" means the whole structure (object/array) ended early. Truncated responses can trigger either.

Fix It Now