No silver bullet.
But you can try defining value as json.RawMessage
in order to delay the parsing till you know what you have got.

See
http://eagain.net/articles/go-dynamic-json/

On Friday, 29 September 2017 00:41:53 UTC+1, Yaroslav Molochko wrote:
>
> I have following json messages:
>
> {"status":"success","data":["val1","val2","val3"]}
>
> {"status":"success","data":{"resultType":"vector","result":[{"metric":{"host_name":"hss4"},"value":[1504157313.787,"24"]},{"metric":{"host_name":"hss508"},"value":[1504157313.787,"32"]}]}}
>
> where *data* can have several formats:
> first one can be interpreted as:
>
> []string
>
> second line's data can be parsed as:
>
> type Data struct {
> ResultType string   `json:"resultType"`
> Result    struct {
>
> Metric map[string]string `json:"metric"`
> Values Values            `json:"values,omitempty"`
> Value  interface{}       `json:"value,omitempty"`
>
>         }
> }
>
> I need to parse those JSON strings, how can I do that? I've tried to make 
> some parser function, the only way I come to was to create some custom 
> unmarshaling function, which unmarshals one type, and if there is no such 
> thing tries to unmarshal using another struct. 
> I believe there must be some other way around to make it more straight 
> forward. 
>
> Any suggestions will be highly appreciated. 
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to