Search code examples
jsongointegerterraform

json: cannot unmarshal number 5088060241 into struct of type int


I'm working on a Terraform project using the OVH provider, when the record is created, the provider is unable to fetch the ID of the record and trigger this error : cannot unmarshal number 5088060240 into Go struct field OvhDomainZoneRecord.id of type int

I opened an issue on the github repository but still waiting for an answer. I would like to correct the problem by myself but i'm not a Go developper and i canno't find any related error.

The struct of OvhDomainZoneRecord :

type OvhDomainZoneRecord struct {
    Id        int    `json:"id,omitempty"`
    Zone      string `json:"zone,omitempty"`
    Target    string `json:"target"`
    Ttl       int    `json:"ttl,omitempty"`
    FieldType string `json:"fieldType"`
    SubDomain string `json:"subDomain,omitempty"`
}

The related file : https://github.com/terraform-providers/terraform-provider-ovh/blob/master/ovh/resource_ovh_domain_zone_record.go


Solution

  • Size of int is either 32 or 64 bit depending on the target architecture you compile to and run on. Your input 5088060240 is bigger than the max value of a 32-bit integer (which is 2147483647), so if your int is 32-bit, you get this error.

    Easiest fix is to use int64. See this example:

    var i int32
    fmt.Println(json.Unmarshal([]byte("5088060240"), &i))
    
    var j int64
    fmt.Println(json.Unmarshal([]byte("5088060240"), &j))
    

    Output (try it on the Go Playground):

    json: cannot unmarshal number 5088060240 into Go value of type int32
    <nil>