Skip to content

Instantly share code, notes, and snippets.

@maniankara
Created August 24, 2018 22:21
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 maniankara/6ab874cbb377b550ea3d83d46e5557bb to your computer and use it in GitHub Desktop.
Save maniankara/6ab874cbb377b550ea3d83d46e5557bb to your computer and use it in GitHub Desktop.
Unmarshalling a arbitrary json list. There are several examples of nested list but not at root
// try here: https://play.golang.org/p/SChzGHXYgOU
package main
import (
"encoding/json"
"fmt"
)
func main() {
birdJson := `{"birds":[{"pigeon":"likes to perch on rocks","eagle":"bird of prey"}]}`
var result map[string]interface{}
json.Unmarshal([]byte(birdJson), &result)
birds := result["birds"].([]interface{})
for _, key := range birds {
t := key.(map[string]interface{})
for k, v := range t {
fmt.Println(k,v)
//pigeon likes to perch on rocks
//eagle bird of prey
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment