Skip to main content

Unterminated string in JSON (and bad control character in string literal)

Paste the JSON that fails and click Validate to find the string that breaks it

14
Loading editor…

What the errors mean

A JSON string starts and ends with a double quote, may not contain a raw line break or tab, and writes a double quote or backslash inside it as \" or \\. Break any of those rules and the parser reports one of: "Unterminated string in JSON at position N" (it reached the end of the text without finding the closing quote), "Bad control character in string literal in JSON at position N" (a raw newline or tab inside a string), or "Bad escaped character in JSON at position N" (a backslash followed by something that is not an escape). Python words them "Unterminated string starting at", "Invalid control character at" and "Invalid \escape".

The editor above opens on a string with a raw line break. Click Validate to see the line it reports.

Causes and fixes

Messages are from Node.js; Python's wording is given in brackets where it differs.

Missing closing quote

Error: Unterminated string in JSON at position 13 [Python: Unterminated string starting at: line 1 column 7 (char 6)]

Invalid

{"a": "hello}

Fixed

{"a": "hello"}

The string opened at "hello was never closed, so the parser read to the end of the text. Add the closing quote.

Raw line break inside a string

Error: Bad control character in string literal in JSON at position 12 [Python: Invalid control character at: line 1 column 13 (char 12)]

Invalid

{"a": "line1
line2"}

Fixed

{"a": "line1\nline2"}

Strings cannot span lines. Write the line break as the two characters \n. Tabs need \t. When you build the JSON in code, let json.dumps or JSON.stringify do the escaping.

Unescaped double quote inside a string

Error: Expected ',' or '}' after property value in JSON at position 12 [Python: Expecting ',' delimiter]

Invalid

{"a": "say "hi" now"}

Fixed

{"a": "say \"hi\" now"}

The second quote closes the string early, and the parser then finds hi where a comma should be. Escape quotes inside strings with a backslash.

Backslashes in a Windows path

Error: Bad escaped character in JSON at position 10 [Python: Invalid \escape: line 1 column 10 (char 9)]

Invalid

{"p": "C:\Users\ada"}

Fixed

{"p": "C:\\Users\\ada"}

A backslash starts an escape sequence, and \U is not one. Write each literal backslash as \\, or use forward slashes.

Truncated text

Error: Unterminated string in JSON at position 10

Invalid

{"a": "hel

Fixed

{"a": "hello"}

The text was cut off inside a string: a download that stopped early, a size limit, or a log line that truncates long values. Get the complete text.

These errors mean a string in the JSON is not written the way JSON requires: it is never closed, it contains a raw line break, or it contains an unescaped quote or backslash. The position points into or just after the string.

Frequently Asked Questions

How do I put a line break in a JSON string?

Write it as the two characters backslash and n (\n). The parser turns it back into a real line break. A raw line break inside the quotes is not allowed.

Why does it point at the end of the text and not at the broken string?

For a missing closing quote the parser only finds out when it runs out of text, so the reported position is the end. The string that is not closed is the last one opened before that point.

Which characters must be escaped in a JSON string?

The double quote and the backslash, plus control characters below U+0020 such as newline, tab and carriage return. Everything else, including non-English letters and emoji, can be written as is.

How can I avoid escaping by hand?

Do not build JSON by joining strings. Use JSON.stringify in JavaScript or json.dumps in Python, which escape every value correctly.

Have suggestions or feedback? We're constantly improving FormatChamp based on your comments.