Skip to content

Instantly share code, notes, and snippets.

@jpukg
Forked from madflojo/mapjson.go
Created February 14, 2022 20:59
Show Gist options
  • Save jpukg/ae7a36709604f738315b5fa49ee7a449 to your computer and use it in GitHub Desktop.
Save jpukg/ae7a36709604f738315b5fa49ee7a449 to your computer and use it in GitHub Desktop.
map.vs.structs.mapjson.go
package main
import (
"encoding/json"
"fmt"
)
func main() {
// Create a map to parse the JSON
var data map[string]interface{}
// Define a JSON string
j := `{"name":"example","numbers":[1,2,3,4],"nested":{"isit":true,"description":"a nested json"}}`
// Parse our JSON string
err := json.Unmarshal([]byte(j), &data)
if err != nil {
fmt.Printf("Error parsing JSON string - %s", err)
}
// Print out one of our JSON values
fmt.Printf("Name is %s", data["name"].(string))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment