Skip to main content
If a String column holds an array written in JSON style, such as ["0x0000000a","0x0000000b"], convert it with 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

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:
To keep the parsed array in the table, add a column that is calculated from the string:

Why CAST does not work

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:
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:
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:
JSONExtract parses the string as JSON, including escaped characters, so prefer it over text replacement.