karatelabs/karate · error

Unexpected end of JSON input in string escape

Error message

Unexpected end of JSON input in string escape

What it means

Thrown by JsonParser.parseString when a string contains a backslash escape but the input ends immediately after the backslash (or before the escape character can be read). A JSON string cannot end with a dangling '\', so the parser reports end-of-input at the escape site.

Solutions

  1. Inspect the string at the error position and complete the escape sequence (e.g. change a trailing '\' to '\\' or finish the intended escape).
  2. Escape backslashes properly: in JSON every literal backslash must be written as '\\'.
  3. Ensure the payload is not truncated — read/transfer the full string before parsing.
  4. Build strings with a JSON serializer so escaping is handled automatically.

Example fix

// before
'{"path": "C:\\Users\\temp' // truncated after backslash
// after
'{"path": "C:\\Users\\temp"}'
Defensive patterns

Strategy: validation

Validate before calling

// Reject payloads ending in a dangling backslash before parsing
static boolean endsWithDanglingEscape(String s) {
    if (s == null) return false;
    int bs = 0;
    for (int i = s.length() - 1; i >= 0 && s.charAt(i) == '\\'; i--) bs++;
    return bs % 2 == 1; // odd number of trailing backslashes = dangling escape
}

Try / catch

try {
    return Json.parse(raw);
} catch (JsonSyntaxException e) {
    if (e.getMessage().contains("string escape")) {
        throw new IllegalArgumentException("Input truncated mid-escape; re-fetch full payload");
    }
    throw e;
}

Prevention

When it happens

Trigger: Parsing input like '"abc\' — a string whose final backslash has no escape character (and closing quote) after it, typically because the input was truncated.

Common situations: Windows file paths embedded in JSON and truncated ('C:\\Users\\...'), regex strings cut off, escaped JSON produced by incomplete serialization, copy/paste losing the last characters of an escaped string.

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


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/2922f96c232ef617. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:191

                }
                throw syntaxError("Expected ',' or ']' in array");
            }
        }

        private String parseString() {
            // we know s.charAt(pos) == '"'
            pos++;
            StringBuilder sb = new StringBuilder();
            while (pos < len) {
                char c = s.charAt(pos);
                if (c == '"') {
                    pos++;
                    return sb.toString();
                }
                if (c == '\\') {
                    pos++;
                    if (pos >= len) {
                        throw syntaxError("Unexpected end of JSON input in string escape");
                    }
                    char esc = s.charAt(pos);
                    pos++;
                    switch (esc) {
                        case '"':
                            sb.append('"');
                            break;
                        case '\\':
                            sb.append('\\');
                            break;
                        case '/':
                            sb.append('/');
                            break;
                        case 'b':
                            sb.append('\b');
                            break;
                        case 'f':
                            sb.append('\f');

View on GitHub (pinned to a22eb90246)