Fix JSON Error: Unterminated String

An unterminated string means the JSON parser found the start of a string but never found the matching closing quote. Here's how to track down the cause and fix it.

JSONTech TeamMarch 1, 20254 min read

The Error Message

This error shows up in several variations:

SyntaxError: Unterminated string in JSON at position 23
SyntaxError: Bad escaped character in JSON at position 18
JSON.parse: unterminated string literal at line 3 column 12

They all mean the same thing: a string that was opened with a double quote never got properly closed.

What Causes This Error

1. Missing Closing Quote

The simplest case — you opened a string with " but forgot to close it.

// ❌ Broken — "world never closed
{
  "greeting": "hello world
}
// ✅ Fixed
{
  "greeting": "hello world"
}

2. Unescaped Double Quotes Inside a String

If your string value itself contains a double quote, you need to escape it with a backslash. Otherwise the parser thinks the string ended at that inner quote.

// ❌ Broken — inner quote terminates the string early
{
  "quote": "She said "hello" to him"
}
// ✅ Fixed — inner quotes escaped
{
  "quote": "She said \"hello\" to him"
}

3. Literal Newlines Inside a String

JSON strings cannot contain literal line breaks. If you need a newline character inside a string, use the escape sequence \n. Pasting multi-line text directly into a JSON string value is a common mistake.

// ❌ Broken — literal newline inside the string
{
  "message": "line one
line two"
}
// ✅ Fixed — newline escaped
{
  "message": "line one\nline two"
}

How to Fix It Step by Step

  1. Go to the position from the error message. The character index points to where the parser gave up. The opening quote of the broken string is usually on the same line or the line above.
  2. Check for matching quotes. Every string must start and end with a ". If you see a " with no partner, add the missing closing quote.
  3. Look for unescaped quotes in the value. If the string content itself contains double quotes, escape each one as \".
  4. Check for literal newlines. If the string spans multiple lines in your editor, replace each line break with \n.
  5. Validate again. Fix one string at a time and re-validate — there could be multiple broken strings in the same document.

Prevention Tips

  • Always use JSON.stringify() when embedding user-supplied text into JSON. It automatically escapes quotes and newlines.
  • Be careful when copying text from word processors or HTML — they often contain "smart quotes" (curly quotes) which are not valid JSON delimiters.
  • Use a JSON-aware editor that shows unmatched quotes with syntax highlighting.
  • When building JSON strings manually in code, use template literals or string concatenation with proper escaping rather than writing raw JSON.

Fix it automatically: Paste your broken JSON into our JSON Repair tool — it handles this error and dozens more. Or validate your JSON first with our JSON Validator.

Related Tools