Printf "$T" retrieving wrong type via getJSON

First, the data.GetJSON template function will be deprecated in the next minor release. Instead, capture the data as a resource (page, global, or remote), then pass it through transform.Unmarshal.

Second, Hugo uses Go’s encoding/json package whenever it reads JSON into a data structure. This process is called unmarshaling. From the Go docs for json.Unmarshal:

To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:

bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[​]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null

So, by the time Hugo has access to the data, integers are already floats. To do something else “under the hood” would require us to rewrite this portion of Go’s encoding/json package… that’s not going to happen. As bep mentioned, this will ultimately be handled upstream.

Hugo uses Go’s json.Unmarshal function when:

  • Reading JSON front matter
  • Accessing JSON files via .Site.Data
  • Accessing JSON files via the soon-to-be-deprecated data.GetJSON template function
  • Using the transform.Unmarshal template function with JSON data
  • etc.
1 Like