CANNOT_PARSE_ESCAPE_SEQUENCE (code 25) when a string in JSON input contains a \u escape that it cannot decode. The most frequent case is an unpaired UTF-16 surrogate, such as \ud834 without the second escape that must follow it. The input is malformed, so the lasting fix is in the application that produces it. On the ClickHouse side you can skip the affected rows, or store the bad sequence as literal text by setting input_format_json_throw_on_bad_escape_sequence = 0.
Reproduce the error
In JSON, a character outside the Basic Multilingual Plane is written as two\u escapes that form a UTF-16 surrogate pair: a high surrogate in the range \ud800 to \udbff, followed by a low surrogate in the range \udc00 to \udfff. For example, the musical symbol G clef (U+1D11E) is written as \ud834\udd1e:
while reading the value of key body) and the row of the input (at row 1) where parsing stopped, which helps you find the offending record.
Variants of the message
The same error code is reported with four different messages, depending on how the escape is broken:
An escape with fewer than four hexadecimal digits that is not at the very end of the input can also surface as a different error. The parser takes the four bytes that follow
\u as they are, so a shortened escape consumes the closing quote of the string, and the insert fails with an error such as Cannot parse JSON string: expected closing quote (CANNOT_PARSE_QUOTED_STRING):
Where the bad data comes from
A lone high surrogate usually means that the producer cut a string in the middle of a character. In languages whose strings are sequences of UTF-16 code units (JavaScript, Java, C#), an emoji or any other character outside the Basic Multilingual Plane occupies two units, so truncating a string to a fixed number of units can keep the first half of a pair and drop the second. Fix the truncation in the producer so that it does not split a pair. The options below only decide what ClickHouse does with records that are already broken.Skip the broken rows
To load the valid rows and drop the broken ones, allow a number of parsing errors withinput_format_allow_errors_num or input_format_allow_errors_ratio:
JSONEachRow resumes parsing at the next line, so this works when every row is on its own line.
Store the bad sequence as is
To keep the rows, disableinput_format_json_throw_on_bad_escape_sequence. The setting is enabled by default and is available since version 24.4. When it is disabled, an escape that cannot be decoded is copied into the value as the characters that were in the input:
u and four hexadecimal digits, six bytes instead of a character. When the value is written back as JSON, the backslash is escaped ("G clef: \\ud834"), so the consumers of your data receive the same six characters. You can find such rows later with a regular expression:
String values read by the JSON input formats. A column of the JSON type parses objects differently: an unpaired surrogate inside it is rejected with Cannot parse JSON object here (INCORRECT_DATA), whatever the value of the setting.