Work with Variant, Dynamic, and JSON data types
mainClickHouse Connect supports Variant, Dynamic, and JSON types. Note that the legacy Object('json') type is no longer supported.
Variant Types
- Reading: Values are read as the matching Python type. To preserve the originating member type, enable the
typedformat usingquery_formats={"Variant": "typed"}. This returnsTypedVariant(value, type_name)objects. - Writing: Native inserts select a member based on the Python value type. If multiple members map to the same Python type, use
clickhouse_connect.datatypes.dynamic.typed_variant(value, "TypeName")to select the member explicitly.
Dynamic Types
- Reading: Values are read as the matching Python type.
- Writing: Inserts are currently sent via their String representation.
JSON Types
- Reading: The default format returns Python dictionaries. To receive JSON strings instead, use
query_formats={"JSON": "string"}. - Writing: You can insert values as Python dictionaries or JSON object strings.
Note: Some complex values in JSON or Dynamic columns stored in the shared-data area may be returned as raw bytes if the client cannot decode them.
from clickhouse_connect.datatypes.dynamic import typed_variant
# Explicitly selecting a Variant member
client.insert('table', [[typed_variant(10, 'Int64')]], column_names=['variant_col'])
# Reading JSON as strings instead of dictionaries
result = client.query('SELECT json_col FROM table', query_formats={'JSON': 'string'})