I have a json field in a sqlite3 collection. My schema looks like:
CREATE Table Animals(
id int,
sounds json,
name string
)
I understand the go-sqlite interface does not support the json datatype explicitly. However, my json is quite simple, as all fields are json arrays, eg;
["bark", "woof", "growl"]
["meow", "hiss", "growl"]
So a full record might be:
id sounds name
1 ["bark", "woof", "growl"] Fido
2 ["meow", "hiss", "growl"] Rufus
Using the package:
_ "github.com/mattn/go-sqlite3"
I am able to extract my json field with
var id sql.NullInt64
var name sql.NullString
var sounds []uint8
err := db.QueryRow("SELECT id,name,sounds FROM Animals WHERE id = ?;", 1).Scan(&id, &name, &sounds)
fmt.Println(strconv.Itoa(id) + "|" + name + "|" + strings.Join(sounds, "+"))
// does print correctly:
1|Fido|bark+wood+growl
That is, it seems the sqlite3 json gets stored in a unicode string(?) as a series of...bytes?...that I can convert to string with the String module. I'm additionally interested in the "+" join operation so I can make a query+string+looking+thing out of this for another application downstream.
However, I'd really like to bundle this all up in JSON, and take advantage of JSON unmarshalling/parsing rather than my ad hoc custom prints. When I try:
type Animal struct {
id int `json:"id"`
name sql.NullString `json:"name"`
sounds []uint8 `json:"sounds"`
}
var a Animal
err := db.QueryRow("SELECT id,name,sounds FROM Animals WHERE id = ?;", 1).Scan(&a.id, &a.name, &a.sounds
)
It prints a bona fide array of integers. How can I embed the strings.Join(sounds []uint8) declaration + function transformation combo in my json-enabled type definition?
Additionally, it's not clear to me how to use the []uint8 string in the event the json is a nulled [] or true NULL and further make it robust against these.
Some refs: