Sample:
{
"id": 1
"data": {"1": 2}
}
Struct definition:
type Item struct {
id int `json:"id"`
data interface{} `json:"data"`
}
I need to parse the payload from a http post, so I used interface{} for data, json.Unmarshal() is successful, but gorm produces error while calling db.Create(item):
(sql: converting Exec argument #5's type: unsupported type map[string]interface {}, a map)
Instead, I change from interface{} to string, calling json.Unmarshal() to parse json POST payload produces error.
unmarshal type error: expected=string, got=object
Basically, one requires interface{}, one requires string.
Anyone encountered this?
The solution is defining a custom type that implements
sql.Valuer,sql.Scanner,json.Marshalerandjson.Unmarshalerinterfaces. Sample of my implementation: