Skip to content

Instantly share code, notes, and snippets.

@XI4222-Irshad
Created January 19, 2019 09:41
Show Gist options
  • Save XI4222-Irshad/dd1aa3d32322d041fb92056770f17744 to your computer and use it in GitHub Desktop.
Save XI4222-Irshad/dd1aa3d32322d041fb92056770f17744 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
)
func main() {
//Simple Employee JSON object which we will parse
empJson := `{
"id": 11,
"name": "Irshad",
"department": "IT",
"designation": "Product Manager",
"address": {
"city": "Mumbai",
"state": "Maharashtra",
"country": "India"
}
}`
// Declared an empty interface
var result map[string]interface{}
// Unmarshal or Decode the JSON to the interface.
json.Unmarshal([]byte(empJson), &result)
address := result["address"].(map[string]interface{})
//Reading each value by its key
fmt.Println("Id :", result["id"],
"\nName :", result["name"],
"\nDepartment :", result["department"],
"\nDesignation :", result["designation"],
"\nAddress :", address["city"], address["state"], address["country"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment