Trying to send query output to browser as JSON, but numbers are treated as Base64. Integers prints out correct.
var rows *sqlx.Rows
enc := json.NewEncoder(w)
rows, err = db.Queryx(query)
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
if err := enc.Encode(results); err != nil{
fmt.Fprintf(w,"%s
", results)
}
}
The result from JSON is (id integer, qty numeric / float):
{"ID":1,"QTY":"OC4wMA=="}
{"ID":2,"QTY":"OC4wMA=="}
Without JSON encoding, the numeric column is treated correct as numbers.
EDIT
@mkopriva hope this answer helps:
"if you provide the table definition so we can see how the column is defined, it would also help to see the SQL query so we can see how you're pulling the column from the db"
"ID" int4
"QTY" numeric
SELECT * FROM table
and it would also help if you provide the code and its output which you mentioned as "treating the columns correctly as numbers"
var rows *sqlx.Rows
rows, err = db.Queryx(query)
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
fmt.Fprintf(w,"%s
", results)
}
Gives this result:
map[ID:1 QTY:9.75]
map[ID:2 QTY:7.00]
"can you do fmt.Printf("%T", results["QTY"]) for us? It is highly doubtful that if "QTY" is truly an int or float that the json encoder would marshal it as a base64 string.
without JSON this gives:
[]uint8 []uint8