{"record":{"id":"2922f96c232ef617","repo":"karatelabs/karate","slug":"unexpected-end-of-json-input-in-string-escape","errorCode":null,"errorMessage":"Unexpected end of JSON input in string escape","messagePattern":"Unexpected end of JSON input in string escape","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":191,"sourceCode":"                }\n                throw syntaxError(\"Expected ',' or ']' in array\");\n            }\n        }\n\n        private String parseString() {\n            // we know s.charAt(pos) == '\"'\n            pos++;\n            StringBuilder sb = new StringBuilder();\n            while (pos < len) {\n                char c = s.charAt(pos);\n                if (c == '\"') {\n                    pos++;\n                    return sb.toString();\n                }\n                if (c == '\\\\') {\n                    pos++;\n                    if (pos >= len) {\n                        throw syntaxError(\"Unexpected end of JSON input in string escape\");\n                    }\n                    char esc = s.charAt(pos);\n                    pos++;\n                    switch (esc) {\n                        case '\"':\n                            sb.append('\"');\n                            break;\n                        case '\\\\':\n                            sb.append('\\\\');\n                            break;\n                        case '/':\n                            sb.append('/');\n                            break;\n                        case 'b':\n                            sb.append('\\b');\n                            break;\n                        case 'f':\n                            sb.append('\\f');","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L173-L209","documentation":"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.","triggerScenarios":"Parsing input like '\"abc\\' — a string whose final backslash has no escape character (and closing quote) after it, typically because the input was truncated.","commonSituations":"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.","solutions":["Inspect the string at the error position and complete the escape sequence (e.g. change a trailing '\\' to '\\\\' or finish the intended escape).","Escape backslashes properly: in JSON every literal backslash must be written as '\\\\'.","Ensure the payload is not truncated — read/transfer the full string before parsing.","Build strings with a JSON serializer so escaping is handled automatically."],"exampleFix":"// before\n'{\"path\": \"C:\\\\Users\\\\temp' // truncated after backslash\n// after\n'{\"path\": \"C:\\\\Users\\\\temp\"}'","handlingStrategy":"validation","validationCode":"// Reject payloads ending in a dangling backslash before parsing\nstatic boolean endsWithDanglingEscape(String s) {\n    if (s == null) return false;\n    int bs = 0;\n    for (int i = s.length() - 1; i >= 0 && s.charAt(i) == '\\\\'; i--) bs++;\n    return bs % 2 == 1; // odd number of trailing backslashes = dangling escape\n}","typeGuard":null,"tryCatchPattern":"try {\n    return Json.parse(raw);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"string escape\")) {\n        throw new IllegalArgumentException(\"Input truncated mid-escape; re-fetch full payload\");\n    }\n    throw e;\n}","preventionTips":["Double every literal backslash when embedding paths/regexes in JSON.","Verify content-length or EOF markers when reading JSON from network/files.","Avoid manual string building of escaped content — serialize instead.","Test round-trip: serialize then parse to catch escaping bugs early."],"tags":["json","escape-sequence","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"}