Skip to main content
In ClickHouse, you expand a JSON value into rows by extracting an array from it with a JSON function and unfolding that array with the ARRAY JOIN clause or the arrayJoin function. The equivalent of FLATTEN(input => PARSE_JSON(payload)) in Snowflake is arrayJoin(JSONExtractKeysAndValuesRaw(payload)) when payload is a JSON object, and arrayJoin(JSONExtractArrayRaw(payload)) when it is a JSON array.

Flatten the keys of an object

Create a table with JSON documents stored in a String column:
In Snowflake, a query like SELECT t.id, f.key, f.value FROM events t, LATERAL FLATTEN(input => PARSE_JSON(t.payload)) f returns one row for every top-level key. In ClickHouse, JSONExtractKeysAndValuesRaw returns the keys and values of an object as an Array(Tuple(String, String)), and ARRAY JOIN produces one row for every element of that array:
The values are unparsed JSON text, so strings keep their double quotes. Pass a value to a function such as JSONExtractString or JSONExtractInt to convert it, or use JSONExtractKeysAndValues(payload, 'String') to get all values converted to one type. The same result is available without the ARRAY JOIN clause: SELECT id, arrayJoin(JSONExtractKeysAndValuesRaw(payload)) AS kv FROM events returns each key and value as one tuple. To flatten a nested object instead of the top level (the PATH argument of FLATTEN), add the keys that lead to it as extra arguments, for example JSONExtractKeysAndValuesRaw(payload, 'c').

Flatten the elements of an array

For a JSON array, use JSONExtractArrayRaw. Join arrayEnumerate of the same array to get the position of each element:
Here the array is the value of the key b. When the document itself is an array, omit the key: JSONExtractArrayRaw('[77,88]') returns ['77','88']. arrayEnumerate numbers the elements from 1, while the INDEX column of FLATTEN starts at 0. The row with id = 2 has no key b, so its array is empty and ARRAY JOIN returns nothing for it. To keep such rows, as OUTER => TRUE does in Snowflake, write LEFT ARRAY JOIN instead of ARRAY JOIN. The row is then returned once, with default values (an empty string and 0 here) in the unfolded columns.

Flatten nested objects and arrays recursively

The functions above expand one level. To walk the whole document, as RECURSIVE => TRUE does, use JSONExtractLeafPathsAndValues, which is available from ClickHouse 26.11:
The function returns only scalar leaves (strings, numbers and booleans), with strings unescaped. Unlike a recursive FLATTEN, it has no rows for the intermediate objects and arrays, and it skips null values.

Flatten a column of the JSON data type

If the documents are stored in a column of the JSON data type, the paths are already parsed. JSONAllPaths returns the paths of each row, and JSONAllValues (available from ClickHouse 26.4) returns the values as strings. Both arrays are ordered by path, so you can unfold them together:
Nested objects are flattened into dotted paths, and an array of scalars stays one value. To get the data type of each path instead of its value, use JSONAllPathsWithTypes. When you know which paths you need, read them directly as subcolumns (payload.a, payload.c.d) instead of flattening the document.

Differences from FLATTEN

FLATTEN is a table function with a fixed set of output columns (SEQ, KEY, PATH, INDEX, VALUE, THIS). ClickHouse has no single function with that output. You choose the JSON function that returns the array you need and select the columns yourself: FLATTEN expands an object or an array, whichever it receives. The ClickHouse functions do not: JSONExtractKeysAndValuesRaw returns an empty array for a JSON array, and JSONExtractArrayRaw returns an empty array for a JSON object, so a document of the other shape produces no rows and no error. If a column contains both shapes, tell them apart with JSONType, which returns Object or Array, and unfold each shape with its own function.