JSONB fields are scanned as []byte and serialized to Base64 in JSON, not as objects
#4150
-
Problem DescriptionWhen using For example, a type Category struct {
SpecTemplate []byte `json:"spec_template"`
}When serialized: how should i do? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Use Override the type mapping in version: "2"
sql:
- engine: "postgresql"
queries: ["query.sql"]
schema: ["schema.sql"]
gen:
go:
package: "model"
out: "model"
sql_package: "pgx/v5"
overrides:
- db_type: "jsonb"
go_type: "encoding/json.RawMessage"After regenerating, the field becomes: type Category struct {
SpecTemplate json.RawMessage `json:"spec_template"`
}
If you need the field to be nullable, use For |
Beta Was this translation helpful? Give feedback.
Use
json.RawMessageinstead of[]bytefor JSONB fields.json.RawMessageis[]byteunder the hood but implementsjson.Marshalerto output raw JSON rather than Base64.Override the type mapping in
sqlc.yaml:After regenerating, the field becomes:
json.RawMessagemarshals directly as the raw JSON value (object, array, etc.) witho…