Skip to content

JavaScript · browsers and Node.js

Bad escaped character in JSON at position N

JSON.parse reports Bad escaped character when a backslash inside a string is followed by a character that is not one of the nine escapes JSON allows.

Updated · 2 min read

console
Chrome, Edge, Node.jsSyntaxError: Bad escaped character in JSON at position 18 (line 2 column 17)
Node.js 20SyntaxError: Bad escaped character in JSON at position 18
Node.js 18, older ChromeSyntaxError: Unexpected token l in JSON at position 18
FirefoxSyntaxError: JSON.parse: bad escaped character at line 2 column 17 of the JSON data
SafariSyntaxError: JSON Parse error: Invalid escape character l

Find it in your JSON

Paste the text that failed. The checker points at the exact spot, and Repair fixes most syntax mistakes.

Input · JSON

Invalid JSON

Line 2, column 16

Invalid escape \l in a string.

1{2  "logDir": "D:\logs\api",                ^3  "level": "info"

Fix: Valid escapes are \" \\ \/ \b \f \n \r \t and \uXXXX. To keep a literal backslash, write \\.

Why this happens
Invalid JSONRuns in your browser

Load from a URL

Your browser fetches it directly, so the server must allow cross-origin requests (CORS).

Why it happens

Inside a JSON string, a backslash must start one of nine escapes. In the sample, \l in D:\logs\api is not one of them, so parsing stops at the l. Most common sources first:

1. Windows paths. D:\logs\api breaks on \l. The paths that parse are worse: "C:\new\tables" contains \n and \t, so it loads without an error as C:, a line break, ew, a tab and ables.

2. Regular expressions. {"zip": "^\d{5}$"} fails on \d. Every backslash in a pattern needs doubling in JSON.

3. An escaped apostrophe. "Don\'t retry" is fine in a JavaScript or PHP string and invalid in JSON. Hand-escaped text and PHP’s addslashes() produce it.

4. Escapes from other languages. \x41, \0 and \a come from C, Python and shell strings. JSON writes the first one as \u0041.

How to fix bad escaped character in JSON

Double the backslash, or use forward slashes

{
  "logDir": "D:\\logs\\api",
  "level": "info"
}

Node.js, Python and most Windows programs also accept D:/logs/api.

Let a serializer write the string

import json
print(json.dumps({"logDir": r"D:\logs\api"}))   # {"logDir": "D:\\logs\\api"}

JSON.stringify does the same in JavaScript.

Store regex patterns with doubled backslashes

{"zip": "^\\d{5}$"}
const zipPattern = new RegExp(config.zip);   // /^\d{5}$/
zipPattern.test('02139');                    // true

PHP: json_encode instead of addslashes

echo json_encode(['msg' => "Don't retry"]);   // {"msg":"Don't retry"}

Repair and the escape tool

The JSON validator names the escape, for example Invalid escape \l in a string. JSON Repair deletes the backslash of an invalid escape. That is right for \' and wrong for paths and patterns: it turns D:\logs\api into D:logsapi. For those, double the backslashes yourself, or paste the raw value into the JSON escape tool, which returns "D:\\logs\\api".

Same error in other languages

  • Python: json.decoder.JSONDecodeError: Invalid \escape: line 2 column 16 (char 17). Python points at the backslash, one character before Chrome.
  • Go: invalid character 'l' in string escape code
  • PHP: json_last_error_msg() returns Syntax error.
  • VS Code: Invalid escape character in string. (261)
  • A raw line break or tab inside a string is a different error: Bad control character in string literal.

For other parse errors, see common JSON errors.

Questions

Which escapes does JSON allow?

Nine: \", \\, \/, \b, \f, \n, \r, \t, and \u followed by four hex digits. A backslash before anything else fails.

Why does C:\users give a different error?

After \u the parser expects four hex digits, so C:\users fails as Bad Unicode escape instead. The fix is the same: double the backslash.

Do forward slashes need escaping?

No. \/ is valid but optional, and a plain / works. PHP's json_encode writes \/ by default, which is why some JSON is full of them.

Related errors

All common JSON errors

Esc