Skip to content

Instantly share code, notes, and snippets.

@abtswath
Last active November 11, 2022 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abtswath/9d288c96b22d93cded720486e20d5ed6 to your computer and use it in GitHub Desktop.
Save abtswath/9d288c96b22d93cded720486e20d5ed6 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
)
var s = `
[{
"video": [
1,
2,
3
],
"video_type": "template_a"
},
{
"video": {
"a": {
"aa": []
}
},
"video_type": "template_b"
}]
`
type sliceVideo []int
type objectVideo struct {
A struct {
Aa []any `json:"aa"`
} `json:"a"`
}
type template struct {
VideoType string `json:"video_type"`
VideoRaw json.RawMessage `json:"video"`
Video interface{}
}
func main() {
buf := bytes.NewBuffer([]byte(s))
decoder := json.NewDecoder(buf)
var templates []*template
err := decoder.Decode(&templates)
if err != nil {
log.Fatalln(err)
}
for _, t := range templates {
switch t.VideoType {
case "template_a":
t.Video = &sliceVideo{}
case "template_b":
t.Video = &objectVideo{}
}
err := json.Unmarshal(t.VideoRaw, &t.Video)
if err != nil {
log.Fatalln(err)
}
}
for _, v := range templates {
fmt.Printf("%s: %v\n", v.VideoType, v.Video)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment