I am trying to decode a map[string]interface{} to a struct, but the "hours" field is not getting populated. I am using https://github.com/mitchellh/mapstructure for decoding. This is the struct:

BusinessAddRequest struct {
        Name       string `json:"name"`
        Phone      string `json:"phone"`
        Website    string `json:"website,omitempty"`
        Street     string `json:"street"`
        City       string `json:"city"`
        PostalCode string `json:"postalCode"`
        State      string `json:"state"`
        Hours []struct {
            Day                 string `json:"day"`
            OpenTimeSessionOne  string `json:"open_time_session_one,omitempty"`
            CloseTimeSessionOne string `json:"close_time_session_one,omitempty"`
            OpenTimeSessionTwo  string `json:"open_time_session_two,omitempty"`
            CloseTimeSessionTwo string `json:"close_time_session_two,omitempty"`
        } `json:"hours"`
        Cuisine []string `json:"cuisine,omitempty"`
        BusinessID int `json:"businessId,omitempty"`
        AddressID  int `json:"addressId,omitempty"`
        UserID     int `json:"userId,omitempty"`

And this is the example data:

"name": "Agave ...", "phone": "(408) 000-000", "street": "Abcd", "city": "San", "postalCode": "90000", "state": "CA", "hours": [ "day": "monday", "open_time_session_one": "10:00", "close_time_session_one": "21:00" "cuisine": [ "Mexican, tacos, drinks" "userId": 1

All fields are getting populated except "hours".

It could be that you're decoding multiple times into the same BusinessAddRequest variable. Note this doesn't work well when you have structure elements that are slices or maps (this applies BOTH to the mapstructure package and to encoding/json as well!). Always use an empty new variable. If the repetition happens in a loop, declare the variable that you decode to in the loop body (it will be a new copy every time you run through the loop).

package main
import "encoding/json"
import "fmt"
import "github.com/mitchellh/mapstructure"
/* (your struct declaration not quoting it to save space) */
func main() {
    var i map[string]interface{}
    config := &mapstructure.DecoderConfig{
        TagName: "json",
    plan, _ := ioutil.ReadFile("zzz")
    var data []interface{}
    /*err :=*/ json.Unmarshal(plan, &data)
    for j := 0; j < len(data); j++ {
        i = data[j].(map[string]interface{})
        var x BusinessAddRequest /* declared here, so it is clean on every loop */
        config.Result = &x
        decoder, _ := mapstructure.NewDecoder(config)
        decoder.Decode(i)
        fmt.Printf("%+v\n", x)

(note I had to use a DecoderConfig with TagName="json" to be able to use your structure definition, which is tagged with "json:" and not "mapstructure:").

If this doesn't help, please check your own code and try to find a minimal example similar to what I posted here that reproduces your problem and add it to the question.

Thank You much. I'm reading a JSON file and unmarshalling to the interface. I get a slice of interfaces, then I loop through the slice and convert to map[string]interface using y: = s.Index(i).Interface().(map[string]interface{}). I think I'm losing structure when I do that. Is there a better way to do it. – Pramod Shashidhara Aug 12 '18 at 23:35 Not sure I understood this. What exactly are you un-marshaling to and how can it be a slice of interfaces? (the input is a JSON object, so it cannot unmarshal to a slice). – Leo K Aug 12 '18 at 23:47 okay here is the code for reading from file plan:=ioutil.ReadFile(fileName) var data interface{} err := json.Unmarshal(plan, &data) s := reflect.ValueOf(data) for i := 0; i < s.Len(); i++ { y := s.Index(i).Interface().(map[string]interface{}) ... Above is the code I'm using to read from file and converting to map – Pramod Shashidhara Aug 12 '18 at 23:53 This should work, then. But... keep in mind that the decoder (that of mapstructure as well as that of encoding/json!!) will not clean up the data structure that you decode to. if you re-use the same 'BusinessAddRequest' variable to decode into, the results might not be what you expect. Also, best not to use reflect unless there's no other way - e.g., if you know the input data is an array, json-decode it into []interface{} or (if the array elements are maps for sure), straight into []map[string]interface{}. I'll update the answer to show how to avoid using the same variable twice. – Leo K Aug 13 '18 at 0:14

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.10.28.35271