joe
joe

Reputation: 1146

Golang Json Unmashalling to structure containing interface{}

I have a json unmarshal requirement to a struct containing an interface{}. The inner type is known only at runtime, hence the struct is defined with interface{}. However, the type is populated correctly before passing to json.Unmarshal. But the json.Unmarshal always populates the resulting struct with map[] instead of given type. How do I resolve this to get the correct unmarshal behavior here.

See the simple test code and behavior here :

package main

import (
    "fmt"
//  s "strings"
    "encoding/json"
)

type one struct {
    S       string  `json:"status"` 

    Res interface{} `json:"res"`    
}

type two struct {
    A   string
    B   string
}

func main() {
    t   := []two {{A:"ab", B:"cd",}, {A:"ab1", B:"cd1",}}
    o   := one {S:"s", Res:t}

    js, _ := json.Marshal(&o)
    fmt.Printf("o: %+v\n", o)

    fmt.Printf("js: %s\n", js)

    er := json.Unmarshal(js, &o)
    fmt.Printf("er: %+v\n", er)

    fmt.Printf("o: %+v\n", o)

}

Result:

o: {S:s Res:[{A:ab B:cd} {A:ab1 B:cd1}]}
js: {"status":"s","res":[{"A":"ab","B":"cd"},{"A":"ab1","B":"cd1"}]}
er: <nil>
o: {S:s Res:[map[A:ab B:cd] map[B:cd1 A:ab1]]}

See Code here1

Upvotes: 0

Views: 351

Answers (1)

Peter Stace
Peter Stace

Reputation: 1434

You could create an ancillary one type that holds the expected type. The convert from that ancillary type back to the regular one type.

i.e. (play link):

package main

import (
    "fmt"
    //  s "strings"
    "encoding/json"
)

type one struct {
    S string `json:"status"`

    Res interface{} `json:"res"`
}

type two struct {
    A string
    B string
}

type oneStatic struct {
    S   string `json:"status"`
    Res []two  `json:"res"`
}

func main() {
    t := []two{{A: "ab", B: "cd"}, {A: "ab1", B: "cd1"}}
    o := one{S: "s", Res: t}

    js, _ := json.Marshal(&o)
    fmt.Printf("o: %+v\n", o)

    fmt.Printf("js: %s\n", js)

    var oStatic oneStatic
    er := json.Unmarshal(js, &oStatic)
    fmt.Printf("er: %+v\n", er)

    o = one{oStatic.S, oStatic.Res}
    fmt.Printf("o: %+v\n", o)

}

Upvotes: 2

Related Questions