Dumping MySQL tables to JSON with Golang

后端 未结 6 1953
余生分开走
余生分开走 2021-01-31 10:18

Was putting together a quick dumper for MySQL to JSON in Go. However I find that everything that I retrieve from the database is a []byte array. Thus instead of n

6条回答
  •  被撕碎了的回忆
    2021-01-31 11:01

    Based on the answers here, this is the most efficient code I could come up with. Note that this is outputting each row as a separate JSON array to save key name repetition.

    // OutputJSONMysqlRowsStream outputs rows as a JSON array stream to save ram & output size due to key name repetition
    func OutputJSONMysqlRowsStream(writer http.ResponseWriter, rows *sql.Rows) {
    
        defer rows.Close()
    
        columns, err := rows.Columns()
    
        if err != nil {
            OutputJSONError(writer, "Failed to get column names")
            return
        }
    
        jsonColumns, err := json.Marshal(columns)
    
        if err != nil {
            OutputJSONError(writer, "Failed to encode json of column names")
            return
        }
    
        writer.Header().Set("Content-Type", "application/cal-json-stream; charset=utf-8")
    
        fmt.Fprintln(writer, "{\"status\": \"done\", \"data\":{ \"json_stream_fields\":"+string(jsonColumns)+"}}")
    
        columnCount := len(columns)
        rowDataHolder := make([]interface{}, columnCount)
        rowDataHolderPointers := make([]interface{}, columnCount)
    
        if err != nil {
            log.Println(err)
        }
    
        for rows.Next() {
    
            for i := 0; i < columnCount; i++ {
                rowDataHolderPointers[i] = &rowDataHolder[i]
            }
    
            err := rows.Scan(rowDataHolderPointers...)
    
            if err != nil {
                log.Println(err)
            } else {
    
                for i, value := range rowDataHolder {
                    tempValue, ok := value.([]byte)
    
                    if ok {
                        rowDataHolder[i] = string(tempValue)
                    }
                }
    
                jsonEncoder := json.NewEncoder(writer)
                err = jsonEncoder.Encode(rowDataHolder)
    
                if err != nil {
                    log.Println(err)
                }
            }
        }
    }
    

提交回复
热议问题