Skip to content

Instantly share code, notes, and snippets.

@iaintshine
Created January 29, 2015 11:06
Show Gist options
  • Save iaintshine/209cbb64cdf237d44596 to your computer and use it in GitHub Desktop.
Save iaintshine/209cbb64cdf237d44596 to your computer and use it in GitHub Desktop.
Parsing nested json payloads wrapped in envelope in golang
package main
import "fmt"
import "encoding/json"
type NestedData struct {
Nested string `json:"nested"`
}
type Data struct {
Value int `json:"value"`
NestedValue *NestedData `json:"nested_value"`
}
type Envelope struct {
Data interface{} `json:"data"`
}
func main() {
jsonBlob := []byte(`{"data":{"value":1,"nested_value":{"nested": "test"}}}`)
env := &Envelope{Data: &Data{}}
err := json.Unmarshal(jsonBlob, env)
if err != nil {
fmt.Println(err)
}
fmt.Println(env.Data.(*Data).NestedValue.Nested)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment