T
T
Toster_someone2021-07-19 20:43:11
go
Toster_someone, 2021-07-19 20:43:11

What is the correct way to work with json in Go?

There is json type

{"type": "type_string", "object": {"field1": 1, "field2" : "text"}}

And there's a structure
type Event struct {
  Type    string `json:"type"`
  Object  []byte `json:"object"`
}


which I am trying to parse with
var event Event
err = json.Unmarshal(body, &event)
if err != nil {
    log.Fatal(err)
}


Then, depending on the type, I can parse the contents of the Object field, but I get the error "json: cannot unmarshal object into Go struct field Event.object of type []uint8"

How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-07-19
@Toster_someone

You need to replace Object []byte with Object json.RawMessage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question