Last active
February 26, 2020 17:09
-
-
Save weibeld/929b97662ae84d27466ae5585c0b8513 to your computer and use it in GitHub Desktop.
Decoding JSON to Go values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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