Struct properties with json.Unmarshal(): Why does the first letter have to be capitalized?

Advertisements Following Situation: package main import ( "encoding/json" "fmt" ) func main() { type person struct { Fullname string `json: "fullname"` Age int `json: "age"` } p := person{} err := json.Unmarshal([]byte(`{"fullname": "Michael Jackson", "age": 55}`), &p) fmt.Println(err) // <nil> fmt.Println(p) // {Michael Jackson 55} } Why do I have to write Fullname (instead of,… Read More Struct properties with json.Unmarshal(): Why does the first letter have to be capitalized?

Inconsistency in numbers between json Marshall and Unmarshall in Golang

Advertisements package main import ( "encoding/json" "fmt" ) func t() { a := int64(1<<63 – 1) fmt.Println(a) m := map[string]interface{}{ "balance": a, } data, _ := json.Marshal(m) m1 := make(map[string]interface{}) err := json.Unmarshal(data, &m1) panicIF(err) fmt.Println(int64(m1["balance"].(float64))) } func panicIF(err error) { if err != nil { panic(err) } } Running above code will print: 9223372036854775807… Read More Inconsistency in numbers between json Marshall and Unmarshall in Golang

Golang: struct does not unmarshal int64 field correctly, other fields are correct

Advertisements I have an http endpoint that receives some json that I unmarshal into a struct. All fields unmarshal correctly with the exception of an int64 timestamp field. There is no error, but the value is set to 0 instead of the value in the json. I tried changing to an int but got the… Read More Golang: struct does not unmarshal int64 field correctly, other fields are correct

Custom unmarshalling from flat json to nested struct

Advertisements Lets say I have two struct that are related like this: type SecretUser struct { UserInfo `json:"userInfo"` Password string `json:"password"` } type UserInfo struct { FirstName string `json:"firstName"` LastName string `json:"lastName"` Email string `json:"email"` } And I receive a JSON in this form: { "firstName": "nice", "lastName":"guy", "email":"nice@guy.co.uk", "password":"abc123" } I want to unmarshall… Read More Custom unmarshalling from flat json to nested struct