> ## 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.

# How to parse a string containing an array into an Array column

> How to convert a String that holds a JSON-style array into Array(String) with JSONExtract, why a plain CAST fails for it, and how NULL values are handled.

If a `String` column holds an array written in JSON style, such as `["0x0000000a","0x0000000b"]`, convert it with [`JSONExtract`](https://clickhouse.com/docs/reference/functions/regular-functions/json-functions#JSONExtract): `JSONExtract(s, 'Array(String)')`. A plain `CAST(s AS Array(String))` fails for this input because `CAST` expects string elements in single quotes.

## Example

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

INSERT INTO raw_events VALUES
    (1, '["0x0000000a","0x0000000b","0x0000000c"]'),
    (2, '["it\'s"]'),
    (3, '[]'),
    (4, NULL);
```

```sql theme={null}
SELECT
    id,
    JSONExtract(tags, 'Array(String)') AS tags_array,
    toTypeName(tags_array) AS type
FROM raw_events
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─tags_array───────────────────────────────┬─type──────────┐
1. │  1 │ ['0x0000000a','0x0000000b','0x0000000c'] │ Array(String) │
2. │  2 │ ['it\'s']                                │ Array(String) │
3. │  3 │ []                                       │ Array(String) │
4. │  4 │ []                                       │ Array(String) │
   └────┴──────────────────────────────────────────┴───────────────┘
```

`JSONExtract` returns an empty array for `NULL`, for an empty string and for a string that is not a valid JSON array, so you do not need `ifNull` around the argument. If you have to tell such rows apart from real empty arrays, check the string with `JSONType(tags) = 'Array'`.

The second argument is the type to return, so the same function parses arrays of other types:

```sql theme={null}
SELECT
    JSONExtract('[1, 2, 3]', 'Array(UInt32)') AS numbers,
    JSONExtract('["a", null, "b"]', 'Array(Nullable(String))') AS nullable_strings,
    JSONExtract('[1, "a", null, 2.5]', 'Array(String)') AS mixed;
```

```text theme={null}
   ┌─numbers─┬─nullable_strings─┬─mixed──────────────┐
1. │ [1,2,3] │ ['a',NULL,'b']   │ ['1','a','','2.5'] │
   └─────────┴──────────────────┴────────────────────┘
```

To keep the parsed array in the table, add a column that is calculated from the string:

```sql theme={null}
ALTER TABLE raw_events
    ADD COLUMN tags_array Array(String) MATERIALIZED JSONExtract(tags, 'Array(String)');
```

## Why CAST does not work

[`CAST`](https://clickhouse.com/docs/reference/functions/regular-functions/type-conversion-functions#CAST) from `String` to `Array` parses the text representation that ClickHouse itself uses for arrays, in which strings are enclosed in single quotes: `['a','b']`. Double quotes are not accepted:

```sql theme={null}
SELECT CAST('["0x0000000a","0x0000000b"]' AS Array(String)) AS tags_array;
```

```text theme={null}
Code: 26. DB::Exception: Cannot parse quoted string: expected opening quote ''', got '"': In scope SELECT CAST('["0x0000000a","0x0000000b"]', 'Array(String)') AS tags_array. (CANNOT_PARSE_QUOTED_STRING)
```

`CAST` also does not accept `NULL` when the target type is not `Nullable`. For a `NULL` value it fails with the error `Cannot convert NULL value to non-Nullable type` (`CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN`).

Arrays of numbers have no quotes, so `CAST('[1, 2, 3]' AS Array(UInt32))` works.

## The replaceAll workaround and its limits

You can make the string acceptable for `CAST` by replacing double quotes with single quotes, and `NULL` with an empty array:

```sql theme={null}
SELECT
    id,
    CAST(replaceAll(ifNull(tags, '[]'), '"', '\'') AS Array(String)) AS tags_array
FROM raw_events
WHERE id != 2
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─tags_array───────────────────────────────┐
1. │  1 │ ['0x0000000a','0x0000000b','0x0000000c'] │
2. │  3 │ []                                       │
3. │  4 │ []                                       │
   └────┴──────────────────────────────────────────┘
```

This works only while the values contain no quotes themselves. The row with `id = 2` is excluded above because its value contains an apostrophe, which ends the string early after the replacement:

```sql theme={null}
SELECT CAST(replaceAll('["it\'s"]', '"', '\'') AS Array(String)) AS tags_array;
```

```text theme={null}
Code: 130. DB::Exception: Cannot read array from text, expected comma or end of array, found 's': In scope SELECT CAST(replaceAll('["it\'s"]', '"', '\''), 'Array(String)') AS tags_array. (CANNOT_READ_ARRAY_FROM_TEXT)
```

`JSONExtract` parses the string as JSON, including escaped characters, so prefer it over text replacement.

## Related pages

* [JSON functions](https://clickhouse.com/docs/reference/functions/regular-functions/json-functions.md)
* [`Array(T)` data type](https://clickhouse.com/docs/reference/data-types/array)
* [Type conversion functions](https://clickhouse.com/docs/reference/functions/regular-functions/type-conversion-functions.md)
