Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Created January 8, 2026 11:12
Show Gist options
  • Select an option

  • Save kavirajk/25dd76787c6790af9265a09310cffbe0 to your computer and use it in GitHub Desktop.

Select an option

Save kavirajk/25dd76787c6790af9265a09310cffbe0 to your computer and use it in GitHub Desktop.
json_error.go
func main() {
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{
"127.0.0.1:9000",
},
})
create := `
CREATE OR REPLACE TABLE test_json (
id Int,
data String
) ENGINE = ReplacingMergeTree
ORDER BY (id);
`
ctx := context.Background()
err = conn.Exec(ctx, create)
if err != nil {
panic(err)
}
// Omitting error checks
chCtx := clickhouse.Context(ctx, clickhouse.WithParameters(clickhouse.Parameters{
"id": "1",
"data": `{"foo": "bar", "nested_data": "{\"key\": \"value\", \"count\": 10}"}`,
}))
err = conn.Exec(chCtx, "INSERT INTO default.test_json VALUES ({id:Int}, {data:String})")
if err != nil {
panic(err)
}
// Omitting error checks
batch, _ := conn.PrepareBatch(ctx, "INSERT INTO default.test_json")
_ = batch.Append(
1, `{"foo": "bar", "nested_data": "{\"key\": \"value\", \"count\": 10}"}`,
)
if err := batch.Send(); err != nil {
panic(err)
}
@kavirajk
Copy link
Author

kavirajk commented Jan 8, 2026

And I see the data are ingested in both cases (query params and batch insert).

clickhouse2 :) select * from test_json;

SELECT *
FROM test_json

Query id: b4d7f911-08e1-4aa7-8f41-ddb231efe877

   ┌─id─┬─data─────────────────────────────────────────────────────────────────┐
1. │  1 │ {"foo": "bar", "nested_data": "{\"key\": \"value\", \"count\": 10}"} │
2. │  1 │ {"foo": "bar", "nested_data": "{"key": "value", "count": 10}"}       │
   └────┴──────────────────────────────────────────────────────────────────────┘

2 rows in set. Elapsed: 0.006 sec.

clickhouse2 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment