panic interface conversion interface {} is float64 not int64

I am getting the following error

panic: interface conversion: interface {} is float64, not int64

I am not sure where float64 is coming from here I have type set to int64 but not sure where float64 came from

type AccessDetails struct {
    AccessUuid   string  `json:"access_uuid"`
    Email        string  `json:"email"`
    Refresh      int64    `json:"refresh"`
    Expiry       int64   `json:"expiry"`
    Permission   string  `json:"permission"`
    Scope        string  `json:"scope"`
}

func GetAccessDetails(c *fiber.Ctx) (*AccessDetails, error) {
    ad := &AccessDetails{}

    cookie := c.Cookies("access_token")

    var err error
    token, err := jwt.Parse(cookie, func(token *jwt.Token) (interface{}, error) {
        return []byte(os.Getenv("ACCESS_SECRET")), nil
    })

    if err != nil {
        return nil, err
    }

    payload := token.Claims.(jwt.MapClaims)
    
    ad.Email = payload["sub"].(string)
    ad.AccessUuid = payload["access_uuid"].(string)
    ad.Refresh = payload["refresh"].(int64)
    ad.Expiry = payload["exp"].(int64)
    ad.Permission = payload["permission"].(string)
    ad.Scope = payload["scope"].(string)

    return ad, nil
}

The error seems to be from the line ad.Refresh = payload["refresh"].(int64) I think i just need to know how to convert types from float64 to int64 or vice versa for interfaces {}

I have tried everything to change the type back to int64 but i get one error or the other and now need help to move forward

here is example of what the payload data looks like from the cookie after it is jwt decoded

{
  "access_uuid": "c307ac76-e591-41d0-a638-6dcc2f963704",
  "exp": 1642130687,
  "permission": "user",
  "refresh": 1642734587,
  "sub": "[email protected]"
}

Solution 1:

ad.Refresh = int64(payload["refresh"].(float64))
ad.Expiry = int64(payload["exp"].(float64))

You need to first assert the accurate dynamic type of the interface value and then, if successful, you can convert it to your desired type.

Note that the reason the interface values are float64 is because that's the default setting of the encoding/json decoder when unmarshaling JSON numbers into interface{} values.

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