{"record":{"id":"4100e791446499cc","repo":"github/copilot-sdk","slug":"invalid-escape-sequence-escaped-at-position-po","errorCode":null,"errorMessage":"Invalid escape sequence \\<escaped> at position <pos-2>","messagePattern":"Invalid escape sequence \\\\<escaped> at position <pos-2>","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"java/sdk/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java","lineNumber":1055,"sourceCode":"            }\n            expect('\"');\n            return sb.toString();\n        }\n\n        private char parseEscape() {\n            if (pos >= input.length()) {\n                throw new IllegalArgumentException(\"Unterminated string escape at position \" + pos);\n            }\n            char escaped = input.charAt(pos++);\n            return switch (escaped) {\n                case '\"', '\\\\', '/' -> escaped;\n                case 'b' -> '\\b';\n                case 'f' -> '\\f';\n                case 'n' -> '\\n';\n                case 'r' -> '\\r';\n                case 't' -> '\\t';\n                case 'u' -> parseUnicodeEscape();\n                default -> throw new IllegalArgumentException(\n                        \"Invalid escape sequence \\\\\" + escaped + \" at position \" + (pos - 2));\n            };\n        }\n\n        private char parseUnicodeEscape() {\n            if (pos + 4 > input.length()) {\n                throw new IllegalArgumentException(\"Incomplete Unicode escape at position \" + (pos - 2));\n            }\n            int value = 0;\n            for (int i = 0; i < 4; i++) {\n                char hex = input.charAt(pos++);\n                if (!isAsciiHexDigit(hex)) {\n                    throw new IllegalArgumentException(\"Invalid Unicode escape at position \" + (pos - 1));\n                }\n                int digit = Character.digit(hex, 16);\n                value = (value << 4) | digit;\n            }\n            return (char) value;","sourceCodeStart":1037,"sourceCodeEnd":1073,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/java/sdk/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java#L1037-L1073","documentation":"parseEscape read the character following a backslash and it was not one of the valid JSON escape characters (\" \\ / b f n r t u). Strict JSON defines a closed set of escapes; anything else, such as \\x or \\', is rejected with this IllegalArgumentException, reporting the position of the backslash (pos-2).","triggerScenarios":"Parsing JSON containing an invalid escape like '\\x41' or '\\e'; strings produced by non-JSON escape conventions (Python-style \\x, regex escapes like \\d) embedded directly into JSON strings.","commonSituations":"Hand-writing JSON with regex patterns ('\\d+' unescaped), single-quoted-string habits ('\\''), using string literals from another language without re-escaping; library mix-ups where a non-JSON serializer's output is fed to a strict JSON parser.","solutions":["Replace the invalid escape with the correct JSON one (\\u0041 instead of \\x41) or double the backslash (\\\\d) so it is a literal backslash","Generate the JSON with a serializer rather than hand-concatenation so escaping is handled for you","Search the input at the reported position to see which escape character triggered it","If the content is meant to be a regex or non-JSON escaped string, escape it for JSON on top of its own syntax"],"exampleFix":"// before\nString json = \"{\\\"regex\\\":\\\"\\\\d+\\\"}\"; // \\d is invalid in JSON\n// after\nString json = \"{\\\"regex\\\":\\\"\\\\\\\\d+\\\"}\"; // \\\\d -> literal backslash + d","handlingStrategy":"try-catch","validationCode":"java.util.regex.Matcher m = java.util.regex.Pattern.compile(\"\\\\\\\\\\\\\\\\[^\\\"\\\\\\\\/bfnrtu]\").matcher(json);\nif (m.find()) {\n    throw new IllegalArgumentException(\"Invalid JSON escape at index \" + m.start());\n}","typeGuard":null,"tryCatchPattern":"try {\n    String result = CopilotToolProcessor.jsonToMapOfSource(json);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"Invalid escape sequence\")) {\n        // fix escaping at reported position, e.g. replace \\x with \\u00xx or \\\\\\\\\n    }\n}","preventionTips":["Learn the JSON escape set: only \" \\ / b f n r t u are valid","Escape regex patterns and other language escapes with an extra backslash for JSON","Generate JSON with a serializer rather than by hand","Test hand-built JSON with a reference parser before feeding custom parsers"],"tags":["java","json","parsing","escaping"],"backgroundTag":"json-parse-error","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}