{"record":{"id":"a073d660dce9a088","repo":"karatelabs/karate","slug":"invalid-escape-esc-in-json-string","errorCode":null,"errorMessage":"Invalid escape '\\${esc}' in JSON string","messagePattern":"Invalid escape '\\\\(.+?)' in JSON string","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":224,"sourceCode":"                            sb.append('\\b');\n                            break;\n                        case 'f':\n                            sb.append('\\f');\n                            break;\n                        case 'n':\n                            sb.append('\\n');\n                            break;\n                        case 'r':\n                            sb.append('\\r');\n                            break;\n                        case 't':\n                            sb.append('\\t');\n                            break;\n                        case 'u':\n                            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++) {","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L206-L242","documentation":"Thrown by JsonParser.parseString when a backslash inside a JSON string is followed by a character that is not a valid JSON escape ('\"', '\\\\', '/', 'b', 'f', 'n', 'r', 't', 'u'). RFC 8259 defines a closed set of escape sequences; anything else is invalid.","triggerScenarios":"Parsing JSON strings containing escapes like '\\x41', '\\' (single-quote escape, valid in JS but not JSON), or a bare '\\' before a normal letter such as '\\p' — common when JavaScript/other-language string literals are pasted into JSON.","commonSituations":"Serializing regex patterns or Windows paths without doubling backslashes, using JS-style escapes ('\\'', '\\xNN') in JSON, template output that didn't escape properly, hand-editing escaped strings.","solutions":["Replace the invalid escape with the correct JSON escape — for a literal backslash use '\\\\'.","Remove the backslash if the character should be literal (e.g. \"it's\" needs no escape in JSON).","Convert non-JSON escapes: '\\x41' → '\\u0041'; drop '\\'' → \"'\".","Serialize data with a JSON library instead of writing escaped strings by hand."],"exampleFix":"// before\n'{\"re\": \"a\\x41b\"}'\n// after\n'{\"re\": \"a\\\\x41b\"}' // or '{\"re\": \"a\\u0041b\"}'","handlingStrategy":"validation","validationCode":"// JSON allows only \" \\ / b f n r t u after backslash\nprivate static final java.util.regex.Pattern BAD_ESCAPE =\n    java.util.regex.Pattern.compile(\"\\\\\\\\[^\\\"\\\\\\\\/bfnrtu]\");\nstatic void assertNoBadEscapes(String json) {\n    java.util.regex.Matcher m = BAD_ESCAPE.matcher(json);\n    if (m.find()) throw new IllegalArgumentException(\"Invalid escape at index \" + m.start() + \": \" + m.group());\n}","typeGuard":null,"tryCatchPattern":"try {\n    return Json.parse(raw);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().startsWith(\"Invalid escape\")) {\n        throw new IllegalArgumentException(\"Unsupported escape in JSON string: use \\\\\\\\ for a literal backslash\", e);\n    }\n    throw e;\n}","preventionTips":["Remember JSON escapes are only \\\" \\\\ / \\b \\f \\n \\r \\t \\uXXXX — nothing else.","Convert JS-style escapes (', \\xNN) to JSON equivalents before parsing.","Serialize with a JSON encoder instead of hand-escaping.","Grep fixtures for suspicious escapes like \\x or \\' in CI."],"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"}