I have a struct containing 4 fields:
type Animal struct {
Name string
Age int
Zone int
}
I am doing a post request sending a json object to decode as the struct, the json should look like this:
{
"Age":10,
"Name":"Lion",
"Zone":1,
}
I want all fields to be field, but when ever I don't fill all the fields and send some json like.
{
"Age":10,
"Zone":1,
}
The json.Decoder automatically build that Filed and set it as "" (which is zero value for the type) instead of null.
How can I set null value or check it for being null and generate an error ?
I expect the result to be {Age:10, Zone:1, Name:null} or at least generate an error!
this is the code I use to convert json to struct
animalModel := Animal{}
err := json.NewDecoder(r.Body).Decode(&animalModel)