{"record":{"id":"f53d762f0b95d193","repo":"karatelabs/karate","slug":"invalid-control-character-in-json-string","errorCode":null,"errorMessage":"Invalid control character in JSON string","messagePattern":"Invalid control character in JSON string","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsonParser.java","lineNumber":229,"sourceCode":"                        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++) {\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');","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsonParser.java#L211-L247","documentation":"Thrown by JsonParser.parseString when a raw control character (code point below 0x20, e.g. tab, newline, NUL) appears unescaped inside a JSON string. RFC 8259 forbids literal control characters in strings — they must be written as escapes like '\\n' or '\\t'.","triggerScenarios":"Parsing JSON where a string value contains a literal newline/tab (e.g. multi-line text pasted between quotes), a NUL byte from binary corruption, or un-escaped terminal output captured into a string.","commonSituations":"Copy-pasting multi-line text into a JSON string literal, log files with raw newlines inside values, data fetched from sources that don't escape control chars, corruption from binary reads.","solutions":["Replace literal control characters in the string with their escapes: newline → '\\n', tab → '\\t', carriage return → '\\r'.","Strip or sanitize control characters before parsing if they are noise.","Serialize the source data with a JSON encoder so control chars are escaped automatically.","Check the data source for binary corruption (NUL bytes) and re-fetch clean data."],"exampleFix":"// before\n'{\"text\": \"line1\nline2\"}' // literal newline\n// after\n'{\"text\": \"line1\\nline2\"}'","handlingStrategy":"validation","validationCode":"// Sanitize raw control characters before parsing\nstatic String escapeControlChars(String s) {\n    StringBuilder sb = new StringBuilder(s.length());\n    for (char c : s.toCharArray()) {\n        if (c < 0x20) sb.append(String.format(\"\\\\u%04x\", (int) c));\n        else sb.append(c);\n    }\n    return sb.toString();\n}","typeGuard":null,"tryCatchPattern":"try {\n    return Json.parse(raw);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"control character\")) {\n        return Json.parse(escapeControlChars(raw));\n    }\n    throw e;\n}","preventionTips":["Never paste multi-line text directly into JSON string literals — escape newlines as \\n.","Serialize text data with a JSON encoder, which escapes control chars automatically.","Open files in text (not binary) mode and strip NUL bytes from external data.","Normalize line endings when copying content between systems."],"tags":["json","control-character","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"}