Tell me, please, how to add a JSON parser correctly so that when the lines are unzipped the code does not fall? That is, when compiling, the code encounters text, but not an archive, immediately drops (text and archive in the data field shuffle). You need to write to skip the line with the text. The logic is as follows: Data from the database is json.Unmashal , json.Unmashal or json.NewDecoder (I can not understand what to use), if Unmarshall did not work, it would be err, then if there is err, then you need to unzip and re-do Unmarshall .

 package main import ( "database/sql" "log" _"github.com/go-sql-driver/mysql" "compress/zlib" "bytes" "os" "fmt" "encoding/json" ) func main() { db, err := sql.Open("mysql", "name:password@tcp(127.0.0.1:port)/database") if err != nil { panic(err.Error()) } defer db.Close() rows, err := db.Query(`SELECT data FROM user_stats ORDER BY created_at LIMIT 10`) if err != nil { log.Fatal(err) } defer rows.Close() var data []byte for rows.Next() { err := rows.Scan(&data) if err != nil { log.Fatal(err) } r, err := zlib.NewReader(bytes.NewReader(data)) if err != nil { log.Panicf("Cannot read archive %v", err); } io.Copy(os.Stdout, r) r.Close() } } 

    1 answer 1

     // Задаём структуру нашего JSON type UserStatsData struct { Field1 string `json:"field_1"` Field2 string `json:"field2"` } userStatsData := UserStatsData{} err := json.Unmarshal(data, &userStatsData) if err != nil { // Здесь распаковываете data в []byte, и снова вызываете json.Unmarshal() } 
    • Thank you very much, figured out. - Sergio Hunter