Bad escaped character in JSON strings
SyntaxError: Bad escaped character in JSON at position 16 (line 2 column 15)
Where it stops
{
"path": "C:\Users\ada",
"note": "line oneWhat it means
Inside a JSON string a backslash may be followed by only these: ", \, /, b, f, n, r, t, or u followed by exactly four hex digits. Anything else is a bad escape. Separately, the raw control characters U+0000 to U+001F may never appear literally in a string: they have to be escaped.
Last verified on Chromium 145, Firefox 146, WebKit 26 and Node 20.
It opens with the error marked. Nothing is uploaded. Find where the error is.
- Usually
- Windows file pathsA literal newline inside a stringEscaping a single quoteA truncated unicode escape
- Fix
- Double every literal backslash and replace real newlines and tabs with
\nand\t.
The fix
Double every literal backslash and replace real newlines and tabs with \n and \t. A serializer does this correctly; hand-editing is where it goes wrong.
{
"path": "C:\Users\ada",
"note": "line one
line two"
}Lines 2 and 3 change.
{
"path": "C:\\Users\\ada",
"note": "line one\nline two"
}How the message appears
Chrome and Node first, then Firefox, then Safari. Older versions of Chrome and Node word it differently. Those lines are marked.
- SyntaxError: Bad escaped character in JSON at position 16 (line 2 column 15)Chrome and Node
- SyntaxError: JSON.parse: bad escaped character at line 2 column 15 of the JSON dataFirefox
- SyntaxError: JSON Parse error: Invalid escape character USafari
- SyntaxError: Bad control character in string literal in JSON at position 18 (line 1 column 19)
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 19 of the JSON data
- SyntaxError: JSON Parse error: Unterminated string
- SyntaxError: Bad escaped character in JSON at position 16Older V8 wording
What causes it
Windows file paths
"C:\Users\ada"contains\Uand\a, neither of which is a valid escape. Each backslash has to be doubled:"C:\\Users\\ada".A literal newline inside a string
Pressing Enter inside a quoted value is invalid, however readable it looks. Multi-line text must use
\n; JSON strings cannot span lines.Escaping a single quote
\'is a valid escape in JavaScript and Python and is not one in JSON. A single quote inside a JSON string needs no escape at all.A truncated unicode escape
\u12fails:\urequires exactly four hex digits. Characters outside the basic multilingual plane need a surrogate pair, so an emoji is two\uXXXXescapes, not one.
Worth knowing
NoteLet JSON.stringify build strings that contain paths, regexes or user text. It escapes exactly what needs escaping and nothing else.
NoteA tab pasted into a string value is invisible and produces "Bad control character in string literal". If a string looks fine but fails, check for a literal tab. That message has its own page: the control character page.