Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Created April 25, 2016 19:17
Show Gist options
  • Select an option

  • Save FilBot3/d121011ff1d059160e78914f3f58e93b to your computer and use it in GitHub Desktop.

Select an option

Save FilBot3/d121011ff1d059160e78914f3f58e93b to your computer and use it in GitHub Desktop.
Nested JSON in GOLang
/**
*
*/
package main
import (
"encoding/json"
"fmt"
)
func chkerr(err error) {
if err != nil {
panic(err)
}
}
type Music struct {
Artist string `json:"Artist"`
// Nest or include another struct as an embedded JSON Element
Album // Embedding the Structure Album
Genre string `json:"Genre"`
}
type Album struct {
Year string `json:"Year"`
Track string `json:"Track"`
}
func main() {
fmt.Printf("Showing some nested JSON\n")
message := Music{
Artist: "John Anderson",
Album: Album{
Year: "2016",
Track: "Seminole Wind",
},
Genre: "Country",
}
fmt.Println(message)
json_message, err := json.Marshal(message)
chkerr(err)
fmt.Println(json_message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment