Go json.Unmarshal field case -
i'm new go. trying fetch , marshal json data struct. sample data looks this:
var reducedfielddata = []byte(`[ {"model":"traverse","vin":"1gnkrhkd6ej111234"} ,{"model":"tl","vin":"19uua66265a041234"} ]`)
if define struct receiving data this:
type vehicle struct { model string vin string }
the call unmarshal works expected. however, if use lower case fields ("model" , "vin") matches cases field names in data return empty strings values.
is expected behavior? can convention turned off?
fields need exported (declared uppercase first letter) or reflection library cannot edit them. since json (un)marshaller uses reflection, cannot read or write unexported fields.
so yes, expected, , no, cannot change it. sorry.
you can add tags field change name marshaller uses:
model string `json:"model"`
see the documentation more info on field tags "encoding/json" supports.
Comments
Post a Comment