Skip to content

Instantly share code, notes, and snippets.

@weibeld
Last active February 26, 2020 17:09
Show Gist options
  • Save weibeld/929b97662ae84d27466ae5585c0b8513 to your computer and use it in GitHub Desktop.
Save weibeld/929b97662ae84d27466ae5585c0b8513 to your computer and use it in GitHub Desktop.
Decoding JSON to Go values
// Decode JSON to a string
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`"foo"`)
func main() {
var s string
err := json.Unmarshal(j, &s)
if err != nil {
log.Fatal(err)
}
// Prints: foo
fmt.Printf("%+v\n", s)
}
// Decode JSON to an array
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`["foo","bar","baz"]`)
func main() {
var a []string
err := json.Unmarshal(j, &a)
if err != nil {
log.Fatal(err)
}
// Prints: [foo bar baz]
fmt.Printf("%+v\n", a)
}
// Decode JSON to a map
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`{"foo":1,"bar":2,"baz":3}`)
func main() {
var m map[string]int
err := json.Unmarshal(j, &m)
if err != nil {
log.Fatal(err)
}
// Prints: map[bar:2 baz:3 foo:1]
fmt.Printf("%+v\n", m)
}
// Decode JSON to a map
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`{"name":"Alice","address":{"street":"Wonder Lane 42","city":"Wonderland"},"interests":["K8s","Go","JSON"]}`)
func main() {
var m map[string]interface{}
err := json.Unmarshal(j, &m)
if err != nil {
log.Fatal(err)
}
// Prints: map[address:map[city:Wonderland street:Wonder Lane 42] interests:[K8s Go JSON] name:Alice]
fmt.Printf("%+v\n", m)
}
// Decode JSON to a struct
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`{"Round":3,"Exact":3.14,"hidden":"hidden"}`)
type Number struct {
Round int
Exact float64
hidden string // Non-exported field (lower case) => no assignment
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14 hidden:}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a struct
package main
import (
"encoding/json"
"fmt"
"log"
)
// Note: matching of JSON keys to struct fields is CASE-INSENSITIVE
var j []byte = []byte(`{"round":3,"exact":3.14,"hidden":"hidden"}`)
type Number struct {
Round int
Exact float64
hidden string // Non-exported field (lower case) => no assignment
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14 hidden:}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a struct
package main
import (
"encoding/json"
"fmt"
"log"
)
// Note: matching of JSON keys to struct fields is CASE-INSENSITIVE
var j []byte = []byte(`{"ROUND":3,"exact":3.14,"hidden":"hidden"}`)
type Number struct {
Round int
EXACT float64
hidden string // Non-exported field (lower case) => no assignment
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14 hidden:}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a struct
package main
import (
"encoding/json"
"fmt"
"log"
)
// Note: JSON keys with no matching fields in destination struct are ignored
var j []byte = []byte(`{"round":3,"exact":3.14,"superfluous":"superfluous"}`)
type Number struct {
Round int
Exact float64
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a struct
package main
import (
"encoding/json"
"fmt"
"log"
)
// Note: struct fields with no matching JSON keys get default value
var j []byte = []byte(`{"round":3,"exact":3.14}`)
type Number struct {
Round int
Exact float64
Superfluous int
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14 Superfluous:0}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a struct
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`{"name":"Alice","address":{"street":"Wonder Lane 42","city":"Wonderland"},"interests":["K8s","Go","JSON"]}`)
type Message struct {
Name string
Address Address
Interests []string
}
type Address struct {
Street string
City string
}
func main() {
var m Message
err := json.Unmarshal(j, &m)
if err != nil {
log.Fatal(err)
}
// Prints: {Name:Alice Address:{Street:Wonder Lane 42 City:Wonderland} Interests:[K8s Go JSON]}
fmt.Printf("%+v\n", m)
}
// Decode JSON to a struct with tags
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`{"myRound":3,"myExact":3.14}`)
type Number struct {
Round int `json:"myRound"`
Exact float64 `json:"myExact"`
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a struct with tags
package main
import (
"encoding/json"
"fmt"
"log"
)
// Note: matching of JSON keys to struct tags is CASE-INSENSITIVE
var j []byte = []byte(`{"MYROUND":3,"myExact":3.14}`)
type Number struct {
Round int `json:"myRound"`
Exact float64 `json:"myexACT"`
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a struct with tags
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`{"myRound":3,"myExact":"3.14"}`)
// Note: "string" decodes JSON string to type of struct field
type Number struct {
Round int `json:"myRound"`
Exact float64 `json:"myExact,string"`
}
func main() {
var n Number
err := json.Unmarshal(j, &n)
if err != nil {
log.Fatal(err)
}
// Prints: {Round:3 Exact:3.14}
fmt.Printf("%+v\n", n)
}
// Decode JSON to a generic value
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`{"name":"Alice","address":{"street":"Wonder Lane 42","city":"Wonderland"},"interests":["K8s","Go","JSON"]}`)
func main() {
// Note: this is useful if you don't know the JSON structure beforehand
var f interface{}
err := json.Unmarshal(j, &f)
if err != nil {
log.Fatal(err)
}
// Prints: map[address:map[city:Wonderland street:Wonder Lane 42] interests:[K8s Go JSON] name:Alice]
fmt.Printf("%+v\n", f)
}
// Decode JSON to a generic value
package main
import (
"encoding/json"
"fmt"
"log"
)
var j []byte = []byte(`["foo", "bar", "baz"]`)
func main() {
// Note: this is useful if you don't know the JSON structure beforehand
var f interface{}
err := json.Unmarshal(j, &f)
if err != nil {
log.Fatal(err)
}
// Prints: [foo bar baz]
fmt.Printf("%+v\n", f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment