karatelabs/karate · error

Invalid \u escape in JSON string

Error message

Invalid \u escape in JSON string

What it means

Thrown by JsonParser.parseHex4 when a '\u' escape is encountered but fewer than 4 characters remain in the input after it. JSON requires exactly four hexadecimal digits after '\u', so a truncated escape (e.g. '"\u00"' at end of input) is invalid.

Solutions

  1. Complete the '\u' escape with all four hex digits (e.g. '\u12' → '\u0012').
  2. Restore the truncated portion of the payload — re-fetch or re-read the full content.
  3. Check any preprocessing (slicing, regex extraction) that may cut the string mid-escape.
  4. Validate the JSON with a linter to catch truncated escapes before parsing.

Example fix

// before
'"caf\u00' // truncated escape
// after
'"caf\u00e9"'
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every \u escape has 4 following chars
static boolean hasTruncatedUnicodeEscape(String s) {
    if (s == null) return false;
    int i = s.indexOf("\\u");
    while (i >= 0) {
        if (i + 6 > s.length()) return true; // fewer than 4 chars after \u
        i = s.indexOf("\\u", i + 1);
    }
    return false;
}

Try / catch

try {
    return Json.parse(raw);
} catch (JsonSyntaxException e) {
    if (e.getMessage().contains("\\u escape")) {
        throw new IllegalArgumentException("Truncated unicode escape; re-read full payload", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Parsing strings with truncated unicode escapes like '"\u12' at end of input, or '\u004' followed only by the closing quote region running out — the parser demands pos+4 <= len.

Common situations: Truncated file downloads or HTTP bodies cutting a unicode escape in half, string slicing/regex that chopped the tail, copy-paste dropping trailing hex digits from escapes like '\u00e9'.

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/fbf312939adef305. Report an issue: GitHub.

Appendix: source

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

                            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++) {
                char h = s.charAt(pos + i);
                int d;
                if (h >= '0' && h <= '9') d = h - '0';
                else if (h >= 'a' && h <= 'f') d = 10 + (h - 'a');
                else if (h >= 'A' && h <= 'F') d = 10 + (h - 'A');
                else throw syntaxError("Invalid hex digit '" + h + "' in \\u escape");
                v = (v << 4) | d;
            }
            pos += 4;
            return (char) v;
        }

        private Object parseNumber() {
            int start = pos;
            boolean isFloat = false;

View on GitHub (pinned to a22eb90246)