Querying number fields appears to force float64
Closed this issue · 1 comments
pedromss commented
From the example on the readme:
package main
import "github.com/thedevsaddam/gojsonq"
func main() {
const json = `{"name":{"first":"Tom","last":"Hanks"},"age":61}`
age := gojsonq.New().JSONString(json).Find("age")
println(age.(float64))
}
Why does age
get set as a float64
instead of an int
or int64
? Not very intuitive because looking at the JSON there are no decimal places and the number is small. What's the rationale? Is it always float64
?
thedevsaddam commented
By default Golang set JSON number to float64
, if you need to get the integer value then you can use the Result.As()
method
const json = `{"name":{"first":"Tom","last":"Hanks"},"age":61}`
result, _ := gojsonq.New().JSONString(json).FindR("age") // handle error
var age int
result.As(&age) // handle error
fmt.Printf("%#v\n", age)