Martin Paucot
Martin Paucot

Reputation: 1251

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

Upvotes: 2

Views: 2255

Answers (2)

Samith Orozco
Samith Orozco

Reputation: 1

I got the same error and I'm not a Go expert neither. Don't know how to change the golang to int64 I was using terraform 32-bit version and I got that same error:

error: Error creating droplet: json: cannot unmarshal number # into Go struct field LinkAction.links.actions.id of type int

when creating a resource on another provider (digitalocean).

I was able to solve the error by changing the terraform to 64-bit version. This worked for me

latest: terraform_1.8.5_windows_amd64.zip

Upvotes: -1

icza
icza

Reputation: 418435

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>

Upvotes: 5

Related Questions