Skip to content

Instantly share code, notes, and snippets.

@melvinodsa
Created January 27, 2019 03: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 melvinodsa/463eb7c518e90eb2dd93c5a861d35500 to your computer and use it in GitHub Desktop.
Save melvinodsa/463eb7c518e90eb2dd93c5a861d35500 to your computer and use it in GitHub Desktop.
Parse JSON in Golang
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
)
func main() {
jsonStr := `{
"ID": "Book1",
"Name": "Harry Potter and the Deathly Hallows",
"Author": "J. K. Rowling"
}`
decoder := json.NewDecoder(strings.NewReader(jsonStr))
obj := struct {
ID string
Name string
Author string
}{}
err := decoder.Decode(&obj)
if err != nil {
log.Fatal("Error while parsing the json string", err)
}
fmt.Println("Parsed obj", obj)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment