thedevsaddam / gojsonq

A simple Go package to Query over JSON/YAML/XML/CSV Data
https://github.com/thedevsaddam/gojsonq/wiki
MIT License
2.18k stars 140 forks source link

The type of value is wrong #28

Closed BeanWei closed 5 years ago

BeanWei commented 5 years ago
func main()  {
    text := `{"id": 1827049}`
    j := gojsonq.New().JSONString(text).Find("id")
    fmt.Println("Num: ", j, "\nType: ", fmt.Sprintf("%T", j))
}
output:
Num:  1.827049e+06 
Type:  float64
thedevsaddam commented 5 years ago

This package decode the JSON document in map[string]interface{}, JSON number is decoded in float64 https://github.com/golang/go/blob/master/src/encoding/json/decode.go#L53

You can do it like,

func main() {
    text := `{"id": 1827049}`
    j := gojsonq.New().JSONString(text).Find("id")
    fmt.Println("Num: ", j, "\nValue: ", fmt.Sprintf("%d", uint64(j.(float64))))
}

Output:

Num:  1.827049e+06
Value:  1827049