Original post

I’m parsing JSON data from a command and unmarshaling it into structs. I created the struct using JSONtoGo and I’m stuck at a few keys, because they can either be a string or an int depending on the output.

If I typecast the variable as a string it says that it “can’t unmarshal number into struct field .Arr of type string” and if I typecast it as a int it says “can’t unmarshal string into struct field .Arr of type int”

Here’s the sample JSON

 {
                                "DG" : 0,
                                "Arr" : "-",
                                "Row" : "-",
                                "EID:Slot" : "-",
                                "DID" : "-",
                                "Type" : "RAID1",
                                "State" : "Dgrd",
                                "BT" : "N",
                                "Size" : "185.781 GB",
                                "PDC" : "dflt",
                                "PI" : "N",
                                "SED" : "N",
                                "DS3" : "dflt",
                                "FSpace" : "N",
                                "TR" : "N"
                        },
                        {
                                "DG" : 0,
                                "Arr" : 0,
                                "Row" : "-",
                                "EID:Slot" : "-",
                                "DID" : "-",
                                "Type" : "RAID1",
                                "State" : "Dgrd",
                                "BT" : "N",
                                "Size" : "185.781 GB",
                                "PDC" : "dflt",
                                "PI" : "N",
                                "SED" : "N",
                                "DS3" : "dflt",
                                "FSpace" : "N",
                                "TR" : "N"
                        },

Here’s the struct

                   TOPOLOGY    []struct {
                                DG      int    `json:"DG"`
                                Arr     string `json:"Arr"`
                                Row     string `json:"Row"`
                                EIDSlot string `json:"EID:Slot"`
                                DID     string `json:"DID"`
                                Type    string `json:"Type"`
                                State   string `json:"State"`
                                BT      string `json:"BT"`
                                Size    string `json:"Size"`
                                PDC     string `json:"PDC"`
                                PI      string `json:"PI"`
                                SED     string `json:"SED"`
                                DS3     string `json:"DS3"`
                                FSpace  string `json:"FSpace"`
                                TR      string `json:"TR"`
                        } `json:"TOPOLOGY"`

How can I make this work?