Rows to map to JSON using sqlx package

base64, go, json

Solution

Just iterate over your map and decode the base64 strings prior to marshal the map:

for k, encoded := range m {
    decoded, err := base64.StdEncoding.DecodeString(encoded)
    if err != nil {
            log.Fatal("error:", err)
    }
    m[k] = decoded
}

b, _ := json.Marshal(m)

You must add this to your imports : `"encoding/base64"`.

Problem

Trying to get the result into a JSON string, I have to use `MapScan` because i have no structs that represent the data so here is what i did ``` import ( "fmt" "log" "encoding/json" _ "github.com/jmoiron/sqlx" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sqlx.Connect("mysql", "uname:pwd@/db") if err != nil { log.Fatal(err) } m := map[string]interface{}{} //Go through rows rows, err := db.Queryx("SELECT id,cname FROM items") for rows.Next() { err := rows.MapScan(m) if err != nil { log.Fatal(err) } } //Marshal the map b, _ := json.Marshal(m) //Prints the resulted json fmt.Printf("Marshalled data: %s\n", b) } ``` The output is `Marshalled data: {"cname":"c29tZWl0ZW0","id":"MA=="}` and it should be `Marshalled data: {"cname":"someitem","id":0}` and I am not sure how to go around this since the values returned in `base64` encodig, any ideas?

Original source