Reputation: 503
I'm having trouble wrapping my head around how to unmarshal a raw json string that was from a previously unmarshaled json byte array. I have the current code:
type Message struct {
Event string
Data json.RawMessage // how data is parsed depends on the event
}
type CreateMessage struct {
id int
}
var evt = []byte(`{"event": "create", "data" :{"id":5 }}`)
func main() {
var message Message
log.Println(string(evt))
json.Unmarshal(evt, &message)
log.Println(message)
log.Println(message.Event)
log.Println(string(message.Data))
fmt.Printf("%+v\n", message)
var message2 = new(CreateMessage)
err := json.Unmarshal( message.Data, &message2 )
log.Println(message2)
log.Println(err)
}
And the output is:
2015/06/29 23:22:10 {"event": "create", "data" :{"id":5 }}
2015/06/29 23:22:10 {create [123 34 105 100 34 58 53 32 125]}
2015/06/29 23:22:10 create
2015/06/29 23:22:10 {"id":5 }
{Event:create Data:[123 34 105 100 34 58 53 32 125]}
2015/06/29 23:22:10 &{0}
2015/06/29 23:22:10 <nil>
Why can't I unmarshal data as a CreateMessage
object? I tried the example here and here but they don't unmarshal the nested raw json data and that is exactly what I'm trying to do.
Upvotes: 2
Views: 3069
Reputation: 417767
The problem is simply that the id
field of the CreateMessage
struct is unexported, it starts with a lowercase letter. Change it to:
type CreateMessage struct {
Id int
}
And it will work.
Notes:
Since message2
is already a pointer (new(CreateMessage)
), you don't have to pass its address to json.Unmarshal()
, its value is enough:
var message2 = new(CreateMessage)
if err := json.Unmarshal(message.Data, message2); err != nil {
panic(err)
}
log.Printf("%+v", message2)
Output:
2009/11/10 23:00:00 &{Id:5}
Or don't use new()
at all:
var message2 CreateMessage
if err := json.Unmarshal(message.Data, &message2); err != nil {
panic(err)
}
log.Printf("%+v", message2)
Output:
2009/11/10 23:00:00 {Id:5}
Try it on the Go Playground.
Also note that now the name of the field is "Id"
and JSON contains "id"
but the json
package is "intelligent" enough to match them (same with "Event"
and "event"
). But know that if you would ever try to marshal your structs, the output would contain "Id"
and not "id"
.
If you want to use a completely different field name or make sure it will be lowercased when you marshal your structs, you can use struct tags to tell how it should appear in the JSON text, for example:
type CreateMessage struct {
MyId int `json:"id"`
}
Upvotes: 4