0

I am having some trouble handling data received by my websocket server written in Go using gorilla. I am communicating in JSON, where there is a action_type to every JSON object, but rest of the content is unknown at run time. I currently use conn.ReadJSON(&message) where message := make(map[string]interface{}). The problem is when the JSON looks like this:

{
  "action_type": "test",
  "something": {
    "one": 1,
    "two": "two",
  },
}

How would I go about parsing "something" in to Go variable with defined Go struct:

type Something struct {
  one  int
  two  string
}

s := data["something"].(Something) is not working, returning error: interface conversion: interface {} is map[string]interface {}, not Something.

asd12tgzxvbgt
  • 307
  • 2
  • 13
  • 1
    Use json.RawMessage to capture the varying part of the message. See [Decoding generic JSON objects to one of many formats](https://stackoverflow.com/questions/28033277/decoding-generic-json-objects-to-one-of-many-formats) for a complete explanation. – Charlie Tumahai Oct 31 '20 at 21:12
  • Thanks! 'Decoding generic JSON objects to one of many formats' solved my question excellently. – asd12tgzxvbgt Oct 31 '20 at 21:43

0 Answers0