I have a struct slice that I'm trying to marshal into a json array for insert into a postgresql database with a column type of jsonb[].
type t struct {
A int
B string
}
a := t{1, "hello"}
b := t{2, "goodbye"}
var c []t
c = append(c, a, b)
bytes, _ := json.Marshal(c)
stmt := `INSERT INTO test (s, ja) VALUES ($1, $2)`
_, err = db.Exec(stmt, "test", bytes)
if err != nil {
log.Fatal("Insert error:", err)
}
I get the error:
pq: malformed array literal: "[{"A":1,"B":"hello"},{"A":2,"B":"goodbye"}]"
How can I make the data appropriate for a jsonb[] column?