'Golang - Cannot unmarshal number into Go value of type string

When trying to json.Unmarshal some JSON code from a website into a struct I created, I receive the following error:

cannot unmarshal number into Go value of type string

Here is my code: https://play.golang.org/p/-5nphV9vPw



Solution 1:[1]

There are multiple errors in the struct definition. Here is fixed version.

https://play.golang.org/p/O39E3CdKes

Solution 2:[2]

Basically, there are some problems with the structs; One might want to use JSON-to-Go

There is a particular problem, I encountered is something like this:

json: cannot unmarshal number into Go struct field id of type string

Which means Go can't marshal a JSON number, like "id": 35, into a Go string property

  • it's either id becomes a string "id": "35"; or
  • change the id type in to an int type
type movie struct {
    Genres []struct {
        Id   int
        Name string
    }
}

https://go.dev/play/p/6m_Sz9s7GoT

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CrazyCrow
Solution 2 go je jo