karatelabs/karate · error
Invalid escape '\ ' in JSON string
Error message
Invalid escape '\${esc}' in JSON string What it means
Thrown by JsonParser.parseString when a backslash inside a JSON string is followed by a character that is not a valid JSON escape ('"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u'). RFC 8259 defines a closed set of escape sequences; anything else is invalid.
Solutions
- Replace the invalid escape with the correct JSON escape — for a literal backslash use '\\'.
- Remove the backslash if the character should be literal (e.g. "it's" needs no escape in JSON).
- Convert non-JSON escapes: '\x41' → '\u0041'; drop '\'' → "'".
- Serialize data with a JSON library instead of writing escaped strings by hand.
Example fix
// before
'{"re": "a\x41b"}'
// after
'{"re": "a\\x41b"}' // or '{"re": "a\u0041b"}' Defensive patterns
Strategy: validation
Validate before calling
// JSON allows only " \ / b f n r t u after backslash
private static final java.util.regex.Pattern BAD_ESCAPE =
java.util.regex.Pattern.compile("\\\\[^\"\\\\/bfnrtu]");
static void assertNoBadEscapes(String json) {
java.util.regex.Matcher m = BAD_ESCAPE.matcher(json);
if (m.find()) throw new IllegalArgumentException("Invalid escape at index " + m.start() + ": " + m.group());
} Try / catch
try {
return Json.parse(raw);
} catch (JsonSyntaxException e) {
if (e.getMessage().startsWith("Invalid escape")) {
throw new IllegalArgumentException("Unsupported escape in JSON string: use \\\\ for a literal backslash", e);
}
throw e;
} Prevention
- Remember JSON escapes are only \" \\ / \b \f \n \r \t \uXXXX — nothing else.
- Convert JS-style escapes (', \xNN) to JSON equivalents before parsing.
- Serialize with a JSON encoder instead of hand-escaping.
- Grep fixtures for suspicious escapes like \x or \' in CI.
When it happens
Trigger: Parsing JSON strings containing escapes like '\x41', '\' (single-quote escape, valid in JS but not JSON), or a bare '\' before a normal letter such as '\p' — common when JavaScript/other-language string literals are pasted into JSON.
Common situations: Serializing regex patterns or Windows paths without doubling backslashes, using JS-style escapes ('\'', '\xNN') in JSON, template output that didn't escape properly, hand-editing escaped strings.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- Unexpected end of JSON input in string escape
- Expected ':' after object key
- Expected string key in object
- Invalid control character in JSON string
- Invalid literal — expected 'false'
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/a073d660dce9a088.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:224
sb.append('\b');
break;
case 'f':
sb.append('\f');
break;
case 'n':
sb.append('\n');
break;
case 'r':
sb.append('\r');
break;
case 't':
sb.append('\t');
break;
case 'u':
sb.append(parseHex4());
break;
default:
throw syntaxError("Invalid escape '\\" + esc + "' in JSON string");
}
continue;
}
if (c < 0x20) {
throw syntaxError("Invalid control character in JSON string");
}
sb.append(c);
pos++;
}
throw syntaxError("Unterminated JSON string");
}
private char parseHex4() {
if (pos + 4 > len) {
throw syntaxError("Invalid \\u escape in JSON string");
}
int v = 0;
for (int i = 0; i < 4; i++) {View on GitHub (pinned to a22eb90246)