thedevsaddam / gojsonq

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

Querying number fields appears to force float64 #77

Closed pedromss closed 4 years ago

pedromss commented 4 years ago

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 4 years ago

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)