JSONRepair

Bad Escaped Character in JSON: Why Your Regex Breaks the Parser

How to fix the JSON parse error "bad escaped character in JSON": the backslash just before the reported position is followed by a character JSON does not allow. Double the backslash, or stop hand-writing JSON and build it with a serializer instead.

I had a config file for form validation, and it held one sensible line:

{ "zipPattern": "^\d{3}-\d{4}$" }

Firefox answered with SyntaxError: JSON.parse: bad escaped character in JSON at position 18. I counted to position 18. It was the letter d. Just a d, sitting there, completely innocent.

The d was innocent. The backslash before it was the criminal. A backslash inside a JSON string is only legal when the next character is one of eight: the double quote, the backslash itself, the forward slash, b, f, n, r, t, plus u followed by four hex digits for Unicode. That is the entire list, frozen in RFC 8259. JavaScript strings accept more escapes than JSON does. Python strings accept more too. The overlap is where everyone gets hurt, because code that is perfectly fine in a JS string literal becomes invalid the moment you paste it into JSON.

Where the bad backslash usually comes from

Regexes are the classic source. \d, \w, \s, and \b are the bread and butter of patterns, and none of them are JSON escapes. The parser does not know you meant "digit." It knows a backslash was followed by a letter it does not recognize, and it stops right there. My zip code pattern above died on the first \d at position 18.

Windows paths are the second source, and they are meaner because they fail two different ways. In "C:\Users\new\temp", the \U is an invalid escape and throws. But \n and \t are valid escapes meaning newline and tab, so a path like "C:\new\temp" parses without complaint and quietly becomes something else entirely. The error is the lucky outcome. The silent rewrite is the one that costs you an afternoon.

The third source is escapes borrowed from other languages: \x41 hex escapes, \v for vertical tab, \0. All valid in JavaScript string literals, all invalid in JSON. If your JSON started life as a pasted JS string, these ride along and detonate on parse.

If you landed here because your error came from a Windows path and you do not care about regexes, the fix below applies to you exactly the same way. A path is just a regex with worse luck.

How to fix the "bad escaped character" parse error

The mechanical fix is to escape the backslash itself. The pattern becomes:

{ "zipPattern": "^\\d{3}-\\d{4}$" }

Now the JSON text holds two backslashes before each d. The parser reads \\ as one literal backslash, and your application receives the regex it expected: ^\d{3}-\d{4}$. The same rule repairs the path: "C:\\Users\\new\\temp". Two backslashes in the text, one in the value, every time.

The fix I actually use is to stop writing JSON by hand. JSON.stringify in JavaScript and json.dumps in Python produce the doubled backslashes for you, every time, without counting characters. Hand-built JSON is where every one of these errors is born. The serializer is the guardrail.

And when the data has to live in a config file that a human edits, I keep regexes out of it now. The config holds a name like "us-zip", and the code maps the name to the pattern. That is an opinion you are allowed to disagree with. It has ended this class of bug for me, which is the only metric I trust.

When the error gives you a position number, open the file, go to that position, and move one character left. The backslash sitting there is the thing to fix. Get that right and the position number stops mattering.

Not sure which backslash is the culprit?

Paste your broken JSON into the repair tool

Frequently asked questions

Why does \n work in JSON but \d does not?

\n is on JSON's fixed escape list in RFC 8259; \d is not. The list is \", \\, \/, \b, \f, \n, \r, \t and \uXXXX. A backslash followed by anything else is a parse error, no matter how sensible the character looks.

Is \v a valid JSON escape?

No. \v (vertical tab) is valid inside a JavaScript string literal but not inside JSON. If your JSON was pasted from JavaScript source, these extra escapes come along for the ride and break the parse.

Why do my backslashes double when I copy JSON out of the console?

You are looking at two different representations: the string's characters and the JSON encoding of those characters. Each single backslash in the string becomes two backslashes in the JSON text, because the JSON text has to encode the backslash itself.

Can a JSON repair tool fix bad escapes automatically?

Sometimes, but the guess can be wrong. Doubling the backslash is the safe repair for \d or \x, but in "C:\new" the \n could be a newline or a broken path segment, and no tool can tell which you meant. Treat auto-repair of escapes as a draft and check the result.

Does Python report this error differently?

Yes. Python's json module raises json.decoder.JSONDecodeError: Invalid \escape: line 1 column X (char Y). Same cause, same fix: the backslash is followed by a character that JSON does not allow.

Get one practical dev-tools guide a week: subscribe to the newsletter for new JSON guides and parser explainers.

Related reading: Bad Control Character in String Literal: The Invisible Newline That Breaks JSON · Fixing Unescaped Quotes in JSON String Values · Why Is My JSON Invalid? · Smart Quotes in JSON: Why Your Parser Chokes on Curly Quotes