package main import ( "database/sql/driver" "encoding/json" "errors" "gorm.io/gorm" ) type Dude struct { gorm.Model Name string `gorm:"type:varchar(50)" json:"name"` Email string `gorm:"type:varchar(50)" json:"email"` Metadata JSONB `gorm:"type:jsonb" json:"metadata"` } // JSONB Interface for JSONB Field of Dude Table type JSONB map[string]interface{} // Value Marshal func (a JSONB) Value() (driver.Value, error) { return json.Marshal(a) } // Scan Unmarshal func (a *JSONB) Scan(value interface{}) error { b, ok := value.([]byte) if !ok { return errors.New("type assertion to []byte failed") } return json.Unmarshal(b, &a) }