> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Resolving "Cannot parse escape sequence: missing second part of surrogate pair" when inserting JSON

> Why a JSON insert fails with CANNOT_PARSE_ESCAPE_SEQUENCE (code 25) on an unpaired surrogate or a broken unicode escape, and how to skip such rows or store them as is.

ClickHouse raises `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`:

```sql theme={null}
CREATE TABLE messages
(
    id UInt32,
    body String
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO messages FORMAT JSONEachRow {"id":1,"body":"G clef: \ud834\udd1e"}
```

This insert succeeds. If the second half of the pair is missing, the insert fails:

```sql theme={null}
INSERT INTO messages FORMAT JSONEachRow {"id":2,"body":"G clef: \ud834"}
```

```text theme={null}
Code: 25. DB::Exception: Cannot parse escape sequence: missing second part of surrogate pair. In JSON input formats you can disable setting input_format_json_throw_on_bad_escape_sequence to save bad escape sequences as is: (while reading the value of key body): (at row 1)
: While executing ParallelParsingBlockInputFormat. (CANNOT_PARSE_ESCAPE_SEQUENCE)
```

The message names the key (`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:

| String value in the input | Message |
| - | - |
| `"\ud834"`: a high surrogate that is not followed by `\u` | `Cannot parse escape sequence: missing second part of surrogate pair` |
| `"\ud834\u0041"`: a high surrogate followed by an escape that is not a low surrogate | `Incorrect surrogate pair of unicode escape sequences in JSON` |
| `"\u"` when fewer than four bytes of input remain after `\u` | `Cannot parse escape sequence: less than four bytes after \u` |
| `"\ud834\u"` when fewer than four bytes of input remain after the second `\u` | `Cannot parse escape sequence: less than four bytes after \u of second part of surrogate pair` |

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`):

```sql theme={null}
INSERT INTO messages FORMAT JSONEachRow {"id":2,"body":"G clef: \ud8"}
```

```text theme={null}
Code: 26. DB::Exception: Cannot parse JSON string: expected closing quote: (while reading the value of key body): (at row 1)
: While executing ParallelParsingBlockInputFormat. (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 with [`input_format_allow_errors_num`](https://clickhouse.com/docs/reference/settings/formats/input-format#input_format_allow_errors_num) or [`input_format_allow_errors_ratio`](https://clickhouse.com/docs/reference/settings/formats/input-format#input_format_allow_errors_ratio):

```sql theme={null}
INSERT INTO messages SETTINGS input_format_allow_errors_num = 10 FORMAT JSONEachRow
{"id":2,"body":"G clef: \ud834"}
{"id":3,"body":"no escapes here"}
```

```sql theme={null}
SELECT
    id,
    body,
    length(body) AS bytes
FROM messages
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─body────────────┬─bytes─┐
1. │  1 │ G clef: 𝄞       │    12 │
2. │  3 │ no escapes here │    15 │
   └────┴─────────────────┴───────┘
```

After an error, `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, disable [`input_format_json_throw_on_bad_escape_sequence`](https://clickhouse.com/docs/reference/settings/formats/input-format#input_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:

```sql theme={null}
INSERT INTO messages SETTINGS input_format_json_throw_on_bad_escape_sequence = 0 FORMAT JSONEachRow {"id":2,"body":"G clef: \ud834"}
```

```sql theme={null}
SELECT
    id,
    body,
    length(body) AS bytes
FROM messages
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─body────────────┬─bytes─┐
1. │  1 │ G clef: 𝄞       │    12 │
2. │  2 │ G clef: \ud834  │    14 │
3. │  3 │ no escapes here │    15 │
   └────┴─────────────────┴───────┘
```

Nothing is repaired: row 2 now contains a backslash, the letter `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:

```sql theme={null}
SELECT id, body
FROM messages
WHERE match(body, '\\\\u[dD][89abAB][0-9a-fA-F]{2}');
```

```text theme={null}
   ┌─id─┬─body───────────┐
1. │  2 │ G clef: \ud834 │
   └────┴────────────────┘
```

The setting applies to `String` values read by the JSON input formats. A column of the [`JSON`](https://clickhouse.com/docs/reference/data-types/newjson) 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.

## Related pages

* [Format settings for JSON](https://clickhouse.com/docs/reference/formats/JSON/format-settings.md)
* [`JSONEachRow` format](https://clickhouse.com/docs/reference/formats/JSON/JSONEachRow)
