Golang how to build json from DB records?

Good day, can't figure out how to get one big JSON from database records.
Sample code
type struct Statistic {
 string usernumber
 timestamp string
 holdtime string
 position string
 queue string
 originalposition string
 active string
 agent string
}

rows, err := dbStat.Query("SELECT usernumber, timestamp, holdtime, queue, position, originalposition, active agent FROM datatable ORDER BY created_at desc")
if err != nil{
log.Println(err)
}
defer rows.Close()

stats := make([]*Statistic, 0)

for rows.Next() {

 bk := new(Statistic)
 err := rows.Scan(&bk.usernumber, &bk.timestamp &bk.holdtime, &bk.queue, &bk.position, &bk.originalposition, &bk.active, &bk.agent) 
 if err != nil { log.Fatal(err) }

 stats = append(stats, bk)

 for _, bk := range stats {

fmt.Println(bk.agent)

 // there was a transfer of value further in the methods
 // now we need to create one JSON with all the values
}
}


Iterate over the values and to generate one json from work, and that's what used to get one json with all values does not work, do not even know how to Google and where to dig.
June 8th 19 at 17:29
1 answer
June 8th 19 at 17:31
Solution
After the loop you need to convert the stats in json
jsonData, err := json.Marshal(&stats)
But the structures Show no exported fields, so this will not work. It is necessary to make structure fields to be exported, ie with a capital letter to describe them
for rows.Next() {
 bk := new(Statistic)
 err := rows.Scan(&bk.usernumber, &bk.timestamp &bk.holdtime, &bk.queue, &bk.position, &bk.originalposition, &bk.active, &bk.agent) 
 if err != nil { 
 log.Fatal(err) 
}
 stats = append(stats, bk)
}
jsonData, err := json.Marshal(&stats)
- Willie.Berge commented on June 8th 19 at 17:34
In the fields and there was a problem, I keep getting something like [{},{},{}]. Thank you so much. - nikita.Stracke commented on June 8th 19 at 17:37

Find more questions by tags MySQLJSONGo