Work with JSON columns
devFor ClickHouse 24.8+, JSON columns are supported. When reading, they are returned as nested Python dictionaries. When inserting, you can provide either a Python dictionary or a JSON-formatted string.
async def use_json(conn: Connection):
async with conn.cursor() as cursor:
await cursor.execute(
"CREATE TABLE test.events (id UInt32, doc JSON) ENGINE = MergeTree ORDER BY id"
)
await cursor.execute(
"INSERT INTO test.events (id, doc) VALUES",
[
(1, {"user": {"name": "ada"}, "tags": ["a", "b"]}),
(2, '{"user": {"name": "bob"}}'), # JSON text works too
],
)
await cursor.execute("SELECT doc FROM test.events ORDER BY id")
assert await cursor.fetchone() == ({"user": {"name": "ada"}, "tags": ["a", "b"]},)