Here's a quick-glued reflect example. https://play.golang.org/p/blpmIVKhuc4
If you try using switch value.(type) instead of using reflect, bool will be reported as int, fyi, so using reflect here. On Wednesday, 2 June 2021 at 16:02:36 UTC+1 Robert Glonek wrote: > The json is not very strict, while go is. It would be good if the json > struct could potentially be strictly of same type all over the place. > Implementing parser for this json in any language will be a bit of a pain > with some try/except... > > If you really need to do so, you would need to have the Value as type > interface{} and then use Reflect to find out if it's a struct, or bool or > int and cast it accordingly to it's type. But really, would be easier if > the value field was uniform, no matter the language used. > > So in other words: > 1. unmarshal using interface > 2. reflect to find type > 3. cast to a new variable for the select type > > type Autogenerated struct { > AllParameters []struct { > Priority int `json:"priority"` > CreatedAt string `json:"created_at"` > UpdatedAt string `json:"updated_at"` > ID int `json:"id"` Name string `json:"name"` > ParameterType string `json:"parameter_type"` > Value interface{} `json:"value"` > valueAsInt int > valueAsBool bool > valueAsMap map[string]interface{} > } `json:"all_parameters"` > } > > Example here: > https://play.golang.org/p/PWHz_g_jabz > > > On Wednesday, 2 June 2021 at 14:15:50 UTC+1 natxo....@gmail.com wrote: > >> hi, >> >> I have this json back from an api: >> >> { >> "all_parameters": [{ >> "priority": 60, >> "created_at": "2021-06-02 12:15:13 UTC", >> "updated_at": "2021-06-02 12:15:13 UTC", >> "id": 28, >> "name": "network_interface_mapping", >> "parameter_type": "yaml", >> "value": { >> "interface1": "eno1", >> "interface2": "eno2", >> "interface3": "eno3", >> "interface4": "eno4" >> } >> }, >> { >> "priority": 60, >> "created_at": "2021-06-02 12:15:13 UTC", >> "updated_at": "2021-06-02 12:15:13 UTC", >> "id": 27, >> "name": "component_1", >> "parameter_type": "boolean", >> "value": true >> }, >> { >> "priority": 50, >> "created_at": "2021-03-19 14:34:26 UTC", >> "updated_at": "2021-03-19 14:34:26 UTC", >> "id": 15, >> "name": "url1", >> "parameter_type": "string", >> "value": "http://www.example.org/1" >> } >> ] >> } >> >> If I use the json2go service, then it simplifies the values like this: >> >> >> type Autogenerated struct { >> AllParameters []struct { >> Priority int `json:"priority"` >> CreatedAt string `json:"created_at"` >> UpdatedAt string `json:"updated_at"` >> ID int `json:"id"` Name string `json:"name"` >> ParameterType string `json:"parameter_type"` >> Value struct { >> Interface1 string `json:"interface1"` >> Interface2 string `json:"interface2"` >> Interface3 string `json:"interface3"` >> Interface4 string `json:"interface4"` >> } `json:"value"` >> } `json:"all_parameters"` >> } >> >> so the value struct has the values of just the first parameter, in fact. >> The other parameters have different values, could be a boolean or a string. >> >> How can I use this in my client script? Unmarhalling into this struct >> type gives me obviously errors >> >> Error: json: cannot unmarshal object into Go struct field >> .all_parameters.value of type string >> >> Thanks in advance for your input. >> >> Regards, >> Natxo >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/500f7306-920a-4b81-9de0-20958d884d36n%40googlegroups.com.