Fix JSON Error: Invalid Escape Sequence

JSON only supports a small set of escape sequences. Using anything else — like \\x hex codes or bare backslashes — causes a parse error. Here's the full list of what's valid.

JSONTech TeamMarch 1, 20254 min read

The Error Message

SyntaxError: Bad escaped character in JSON at position 31
SyntaxError: Unexpected token '\' in JSON at position 15
JSON.parse: bad escape character at line 2 column 18

This means the parser found a backslash followed by a character that isn't part of JSON's allowed escape set.

Valid JSON Escape Sequences

JSON supports exactly these escape sequences — and nothing else:

SequenceCharacter
\"Double quote
\\Backslash
\/Forward slash (optional)
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tTab
\uXXXXUnicode code point (4 hex digits)

Anything not on this list — including \x, \a, \e, \0, and \' — is invalid in JSON, even though some of these work in JavaScript or other languages.

Common Mistakes

1. Hex Escape Sequences

JavaScript supports \x41(hex for "A"), but JSON does not. Use the Unicode escape \u0041 instead.

// ❌ Broken — \x is not valid in JSON
{
  "char": "\x41"
}
// ✅ Fixed — use \u with 4 hex digits
{
  "char": "\u0041"
}

2. Windows File Paths

This is one of the most common real-world cases. Windows paths use backslashes, and each one must be doubled in JSON.

// ❌ Broken — single backslashes
{
  "path": "C:\Users\Alice\Documents\file.txt"
}
// ✅ Fixed — backslashes escaped
{
  "path": "C:\\Users\\Alice\\Documents\\file.txt"
}

Alternatively, use forward slashes — most Windows APIs accept them:

// ✅ Also valid — forward slashes
{
  "path": "C:/Users/Alice/Documents/file.txt"
}

3. Single-Quote Escape

In JavaScript, \' escapes a single quote inside a single-quoted string. But JSON only uses double quotes, so the single-quote escape \'isn't needed and isn't valid.

// ❌ Broken — \' is not valid in JSON
{
  "text": "it\'s working"
}
// ✅ Fixed — single quote doesn't need escaping
{
  "text": "it's working"
}

How to Fix It Step by Step

  1. Find the backslash. Go to the position from the error message and find the offending \.
  2. Check the character after it. Is it one of the 9 valid escapes listed above? If not, you need to fix it.
  3. Choose the right fix:
    • If it's a literal backslash (like a path), double it: \\
    • If it's a hex code like \x41, convert to Unicode: \u0041
    • If it's an unnecessary escape like \', remove the backslash
  4. Validate the result. Run the fixed JSON through a parser to confirm.

Prevention Tips

  • Always use your language's JSON serializer (JSON.stringify(), json.dumps(), etc.) when embedding strings with special characters.
  • When storing file paths in JSON, either double every backslash or switch to forward slashes.
  • Keep the list of 9 valid JSON escapes bookmarked — if it's not on the list, it needs to be converted.
  • Test JSON that contains paths or special characters on all target platforms, as escape handling bugs often surface only on Windows.

Fix it automatically: Paste your broken JSON into our JSON Repair tool — it handles this error and dozens more. Or validate your JSON first with our JSON Validator.

Related Tools