What's the proper way to convert a json.RawMessage to a struct?

go, json

Solution

As said, the underlying type of `json.RawMessage` is `[]byte`, so you can use a `json.RawMessage` as the data parameter to `json.Unmarshal`.

However, your problem is that you have a pointer (`*json.RawMessage`) and not a value. All you have to do is to dereference it:

err := json.Unmarshal(*out.Hits.Hits[0].Source, &mySyncInfo)

Working example:

package main

import (
    "encoding/json"
    "fmt"
)

type SyncInfo struct {
    Target string
}

func main() {
    data := []byte(`{"target": "localhost"}`)
    Source := (*json.RawMessage)(&data)

    var mySyncInfo SyncInfo
    // Notice the dereferencing asterisk *
    err := json.Unmarshal(*Source, &mySyncInfo)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v\n", mySyncInfo)
}

Output:

{Target:localhost}

Playground: http://play.golang.org/p/J8R3Qrjrzx

Problem

I have this `struct` ``` type SyncInfo struct { Target string } ``` Now I query some `json` data from ElasticSearch. `Source` is of type `json.RawMessage`. All I want is to map `source` to my `SyncInfo` which I created the variable `mySyncInfo` for. I even figured out how to do that...but it seems weird. I first call `MarshalJSON()` to get a `[]byte` and then feed that to `json.Unmarshal()` which takes an `[]byte` and a pointer to my struct. This works fine but it feels as if I'm doing an extra hop. Am I missing something or is that the intended way to get from a `json.RawMessage` to a `struct`? ``` var mySyncInfo SyncInfo jsonStr, _ := out.Hits.Hits[0].Source.MarshalJSON() json.Unmarshal(jsonStr, &mySyncInfo) fmt.Print(mySyncInfo.Target) ```

Original source