Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am facing a case where I had to store dynamic values in the database with key and value pairs given by the user itself.

User gives the key and value, and I interpret it as

{"key": "user Given", "value": "user Given"}

and I add all such things to an array, and I want this array to be read into Go code where the array of objects is read from the Database table.

You can use the JSON Unmarshaler interface, but depending on how you are retrieving the data from MySql, will vary your implementation. But the idea is the same. For this example, I use https://github.com/go-sql-driver/mysql and assuming you want to store the data in an actual JSON field (Mysql >= 5.7), you could do something like this:

type MyField struct {
    Key string `json:"key"`
    Value string `json:"value"`
type MyModel struct {
    ID uint64
    MyFieldName MyField `json:"my_field_name"`
func (m *MyField) Scan(src interface{}) error {
    // The data stored in a JSON field is actually returned as []uint8
    val := src.([]uint8)
    return json.Unmarshal(val, &m)
func main() {
    db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1)/dbname")
    if err != nil {
        panic(err.Error())
    defer db.Close()
    results, err := db.Query("SELECT id, my_field_name FROM my_table")
    if err != nil {
        panic(err.Error())
    for results.Next() {
        var row MyModel
        err := results.Scan(&row.ID, &row.MyFieldName)
        if err != nil {
            panic(err.Error())
        fmt.Println(row.MyFieldName.Key)

A quick hack, not necessarily the most elegant approach, is to use Golang's default JSON Unmarshaler behavior for golang maps:

jstr := `{"key": "userKeyValue", "value": "userValueValue"}`
// declare a map that has a key string and value interface{} so that any values or
// types will be accepted;
jmap := make(map[string]interface{})
err := json.Unmarshal(jstr, &jmap)
if err != nil {
    log.Fatal(err)
for k, v := range jmap {
    fmt.Printf("Key: %v, Value: %v\n", k, v)
    // If you want to directly work with v remember it is an interface{}
    // Thus to use it as a string you must v.(string)
// Will output the following:
// Key: key, Value: userKeyValue
// Key: value, Value: userValueValue

You can now use standard golang map to manipulate or manage the received data... The more elegant approach is to implement the JSON Marshaler and Unmarshaler interfaces for your declared type. These are described in the golang documentation: https://golang.org/pkg/encoding/json/

I believe OP asked about how to deal with json from MySQL (presumably using the json mysql data type). While this addresses how to handle JSON in golang in general, it says nothing about dealing with databases. – Lobsterman Sep 21, 2019 at 14:43

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.