Bad control character in string literal in JSON at position N (line L column C)
A string in your document contains a literal control character: most often a real newline, sometimes a tab. JSON strings cannot contain characters below U+0020 unescaped. A newline has to be written as \n, a tab as \t. The position points at the offending character.
Paste your JSON and see exactly where it breaks
Nothing you paste leaves your browser. The connect-src allowlist makes that a browser guarantee rather than a promise. Check it yourself
What actually causes it
Ranked by how often each one turns out to be the answer.
-
01 Multi-line text pasted directly into a string
SQL, HTML, a log excerpt or an error message with real line breaks in it. The line break ends the string as far as the parser is concerned.
Breaks
{ "query": "SELECT * FROM users" }Works
{ "query": "SELECT *\nFROM users" } -
02 JSON built by string concatenation
This is the root cause behind almost every instance. Hand-assembled JSON does not escape the values you interpolate into it. Serialise the object instead and the problem cannot occur.
Breaks
const body = '{"note": "' + note + '"}';Works
const body = JSON.stringify({ note }); -
03 A tab character from a copy and paste
Tabs are invisible and land in strings easily when copying from a spreadsheet or a terminal. Escape as \t, or strip them before building the document.
-
04 Windows line endings inside a value
A carriage return, U+000D, is also a control character. Text read from a Windows file and embedded in a string carries \r as well as \n.
The same mistake in other runtimes
The underlying problem is identical; only the wording differs. If a colleague reports one of these, they are looking at the same thing you are.
| Python | Invalid control character at: line 1 column 8 (char 7) |
|---|---|
| Java (Jackson) | Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value |
Questions
- Which characters have to be escaped?
- Everything from U+0000 to U+001F, plus the double quote and the backslash. JSON defines short escapes for backspace, form feed, newline, carriage return and tab; anything else in that range needs the \u form, for example \u0000.
- Can I make the parser accept them?
- Jackson has ALLOW_UNESCAPED_CONTROL_CHARS and some parsers have similar switches, but that hides the problem rather than fixing it, and the document stays invalid for every other consumer. Escape at the point where the JSON is produced.
Fix it now
Paste the payload into the tool above, or go straight to the one built for this job.
Repair it automatically