the reason that this error occurs is that, technically, a string cannot be null; the zero value for a string is the empty string. You cannot assign a string to nil.
However, there are two solutions to this problem:
use *string instead of string
define your own nullable string type and unmarshal null to empty string
The first solution is simply to turn your LastName field into a *string. The result is that now you have the possibility of encountering null pointers, but you're actually accurately modeling the information. This is the most "purely correct" way of doing things, but can lead to runtime panics if you're not checking for nil pointers every time you want someone's last name.
Sometimes it's better to just screw the accuracy and do the thing that's more comfortable with your application, especially if you're dealing with some input that you know is dirty and you're going to be transforming it anyway.
You can define a nullable string type, NString, with the semantics of "if the JSON says null, replace it with empty string", as follows:
type Nstring string
func (n *Nstring) UnmarshalJSON(b []byte) (err error) {
if string(b) == "null" {
return nil
}
return json.Unmarshal(b, (*string)(n))
}
of course, now you have to convert to string all over the place. Be aware that, yes, this is throwing away information since we now lose the ability to differentiate between null and empty string in the source input; that may or may not matter to you.
So... you're stuck either converting to string or checking for nil pointers.