Skip to content

Instantly share code, notes, and snippets.

@eyeezzi
Last active January 3, 2018 03:37
Show Gist options
  • Select an option

  • Save eyeezzi/4f75adeebda0b4220e1641713321abda to your computer and use it in GitHub Desktop.

Select an option

Save eyeezzi/4f75adeebda0b4220e1641713321abda to your computer and use it in GitHub Desktop.
How to Read Generic JSON in Golang.
package main
import (
"encoding/json"
"fmt"
)
func main() {
// Go stores JSON data as an array of bytes.
jstr := `{"fname":"john","lname":"doe","age":25}`
b := []byte(jstr)
// In Go, a generic object has type of 'interface{}'
// JSON is either an object of key-value pairs, i.e map[string]interface{}
// ...or an array of objects, i.e []interface{}
var jobj map[string]interface{}
// Unmarshalling => decoding JSON to Go object.
if err := json.Unmarshal(b, &jobj); err != nil {
fmt.Println(err.Error())
}
fmt.Println(jobj["fname"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment