{"record":{"id":"fbf312939adef305","repo":"karatelabs/karate","slug":"invalid-u-escape-in-json-string","errorCode":null,"errorMessage":"Invalid \\u escape in JSON string","messagePattern":"Invalid \\\\u escape in JSON string","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":239,"sourceCode":"                            sb.append(parseHex4());\n                            break;\n                        default:\n                            throw syntaxError(\"Invalid escape '\\\\\" + esc + \"' in JSON string\");\n                    }\n                    continue;\n                }\n                if (c < 0x20) {\n                    throw syntaxError(\"Invalid control character in JSON string\");\n                }\n                sb.append(c);\n                pos++;\n            }\n            throw syntaxError(\"Unterminated JSON string\");\n        }\n\n        private char parseHex4() {\n            if (pos + 4 > len) {\n                throw syntaxError(\"Invalid \\\\u escape in JSON string\");\n            }\n            int v = 0;\n            for (int i = 0; i < 4; i++) {\n                char h = s.charAt(pos + i);\n                int d;\n                if (h >= '0' && h <= '9') d = h - '0';\n                else if (h >= 'a' && h <= 'f') d = 10 + (h - 'a');\n                else if (h >= 'A' && h <= 'F') d = 10 + (h - 'A');\n                else throw syntaxError(\"Invalid hex digit '\" + h + \"' in \\\\u escape\");\n                v = (v << 4) | d;\n            }\n            pos += 4;\n            return (char) v;\n        }\n\n        private Object parseNumber() {\n            int start = pos;\n            boolean isFloat = false;","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L221-L257","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","solutions":["Complete the '\\u' escape with all four hex digits (e.g. '\\u12' → '\\u0012').","Restore the truncated portion of the payload — re-fetch or re-read the full content.","Check any preprocessing (slicing, regex extraction) that may cut the string mid-escape.","Validate the JSON with a linter to catch truncated escapes before parsing."],"exampleFix":"// before\n'\"caf\\u00' // truncated escape\n// after\n'\"caf\\u00e9\"'","handlingStrategy":"validation","validationCode":"// Ensure every \\u escape has 4 following chars\nstatic boolean hasTruncatedUnicodeEscape(String s) {\n    if (s == null) return false;\n    int i = s.indexOf(\"\\\\u\");\n    while (i >= 0) {\n        if (i + 6 > s.length()) return true; // fewer than 4 chars after \\u\n        i = s.indexOf(\"\\\\u\", i + 1);\n    }\n    return false;\n}","typeGuard":null,"tryCatchPattern":"try {\n    return Json.parse(raw);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"\\\\u escape\")) {\n        throw new IllegalArgumentException(\"Truncated unicode escape; re-read full payload\", e);\n    }\n    throw e;\n}","preventionTips":["Always write \\u escapes with exactly four hex digits, zero-padded (\\u0009 not \\u9).","Check for truncation when slicing or streaming JSON content.","Generate escapes with String.format(\"\\\\u%04x\", cp) to guarantee width.","Lint files containing unicode escapes before parsing."],"tags":["json","unicode-escape","parser"],"backgroundTag":"json-parse-error","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}