Bad escaped character in JSON strings

ErrorJavaScript

SyntaxError: Bad escaped character in JSON at position 16 (line 2 column 15)

Where it stops

{
  "path": "C:\Users\ada",
  "note": "line one

What 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 \n and \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.

broken.jsonFails to parse
{
  "path": "C:\Users\ada",
  "note": "line one
line two"
}
JSON.parse stops at line 2, column 15.

Lines 2 and 3 change.

fixed.jsonParses
{
  "path": "C:\\Users\\ada",
  "note": "line one\nline two"
}
JSON.parse reads it.

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.

Console
  • 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

  1. Windows file paths

    "C:\Users\ada" contains \U and \a, neither of which is a valid escape. Each backslash has to be doubled: "C:\\Users\\ada".

  2. 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.

  3. 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.

  4. A truncated unicode escape

    \u12 fails: \u requires exactly four hex digits. Characters outside the basic multilingual plane need a surrogate pair, so an emoji is two \uXXXX escapes, 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.

What you put into the tools never leaves your browser. We count page views and keep them for 90 days. Accept and we also remember this browser, so a return visit is not counted as new. Privacy policy