{"record":{"id":"6efbb9fe88ab4258","repo":"karatelabs/karate","slug":"invalid-hex-digit-h-in-u-escape","errorCode":null,"errorMessage":"Invalid hex digit '${h}' in \\u escape","messagePattern":"Invalid hex digit '(.+?)' in \\\\u escape","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":248,"sourceCode":"                }\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;\n            if (s.charAt(pos) == '-') {\n                pos++;\n                if (pos >= len) {\n                    throw syntaxError(\"Invalid number: bare '-'\");\n                }\n            }\n            // integer part\n            char c = s.charAt(pos);\n            if (c == '0') {","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L230-L266","documentation":"Thrown by JsonParser.parseHex4 when one of the four characters following a '\\u' escape is not a hexadecimal digit (0-9, a-f, A-F). JSON's '\\u' escape requires exactly four hex digits, so characters like 'g', 'z', or '/' are rejected.","triggerScenarios":"Parsing strings with malformed unicode escapes like '\\u00gg', '\\u12x4', or '\\u abcd' — any non-hex character in the four-digit position after '\\u'.","commonSituations":"Hand-typed unicode escapes with typos, escaping confusion where a '\\u' was meant literally (e.g. Windows '\\Users' paths in JSON should be '\\\\Users'), generated escapes truncated to fewer than 4 digits.","solutions":["Correct the escape to exactly four hex digits, e.g. '\\u00gg' → '\\u0047'.","If the backslash should be literal (like a Windows path), double it: '\\\\Users' instead of '\\Users'.","Pad short escapes to four digits: '\\u9' → '\\u0009'.","Generate unicode escapes programmatically (String.format('\\\\u%04x', codePoint)) to avoid typos."],"exampleFix":"// before\n'{\"ch\": \"\\u00gg\"}'\n// after\n'{\"ch\": \"\\u0047\"}'","handlingStrategy":"validation","validationCode":"// Verify every \\u escape is followed by exactly 4 hex digits\nprivate static final java.util.regex.Pattern U_ESCAPE =\n    java.util.regex.Pattern.compile(\"\\\\\\\\u([0-9a-fA-F]{4})\");\nstatic boolean allUnicodeEscapesValid(String s) {\n    java.util.regex.Matcher m = java.util.regex.Pattern.compile(\"\\\\\\\\u.{0,4}\").matcher(s);\n    while (m.find()) {\n        if (!U_ESCAPE.matcher(m.group()).matches()) return false;\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n    return Json.parse(raw);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"hex digit\")) {\n        throw new IllegalArgumentException(\"Malformed \\\\uXXXX escape; expected 4 hex digits\", e);\n    }\n    throw e;\n}","preventionTips":["Use only 0-9/a-f/A-F in \\u escapes and always four of them.","Double backslashes for literal Windows paths (\\\\Users) so they aren't read as escapes.","Zero-pad short code points: tab is \\u0009, not \\u9 or \\utab.","Test fixtures with a JSON linter that validates escape sequences."],"tags":["json","unicode-escape","hex"],"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"}