I'm trying to set up a Go MySQL server that queries data from the database and sends it as a JSON. My database contains some columns that are in the new JSON type.
The Map struct:
type Map struct{
Id int `json:"id"`
Data string `json:"data"` //This column is stored in the database as a JSON. Which type to use here?
Created time.Time `json:"created"`
UserId int `json:userid`
}
The function to fetch data from database
func GetMap(id int) Map{
var mapId int
var data string //which type should this be
var userId int
var created time.Time
err := _getMap.QueryRow(id).Scan(&mapId, &data, &userId, &created) //getMap is a prepared query
mapObj := Map{Id:mapId, Data:data, UserId:userId, Created:created}
return mapObj
}
When I send this data out like this
resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
resp.WriteHeader(http.StatusOK)
gmap := GetMap(id)
err := json.NewEncoder(resp).Encode(gmap);
The Data in the JSON that the client receives is formatted as a string:
{\"field\":\"nowork\"...}
I think the problem is caused by a wrong datatype in the struct definition, which means that the Data will be parsed in the wrong format.
I have tried to change the datatype to []uint8, interface{} and some others that I thought could be relevant, but to no avail.