Skip to content

Instantly share code, notes, and snippets.

@kelby
Created September 22, 2018 02:05
Show Gist options
  • Save kelby/eb872563775c243d0aaed6724204c437 to your computer and use it in GitHub Desktop.
Save kelby/eb872563775c243d0aaed6724204c437 to your computer and use it in GitHub Desktop.
Parsing JSON files With Golang
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
// Users struct which contains
// an array of users
type Users struct {
Users []User `json:"users"`
}
// User struct which contains a name
// a type and a list of social links
type User struct {
Name string `json:"name"`
Type string `json:"type"`
Age int `json:"Age"`
Social Social `json:"social"`
}
// Social struct which contains a
// list of links
type Social struct {
Facebook string `json:"facebook"`
Twitter string `json:"twitter"`
}
func main() {
// Open our jsonFile
jsonFile, err := os.Open("users.json")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// we initialize our Users array
var users Users
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &users)
// we iterate through every user within our users array and
// print out the user Type, their name, and their facebook url
// as just an example
for i := 0; i < len(users.Users); i++ {
fmt.Println("User Type: " + users.Users[i].Type)
fmt.Println("User Age: " + strconv.Itoa(users.Users[i].Age))
fmt.Println("User Name: " + users.Users[i].Name)
fmt.Println("Facebook Url: " + users.Users[i].Social.Facebook)
}
}
{
"users": [
{
"name" : "Elliot",
"type" : "Reader",
"age" : 23,
"social" : {
"facebook" : "https://facebook.com",
"twitter" : "https://twitter.com"
}
},
{
"name" : "Fraser",
"type" : "Author",
"age" : 17,
"social" : {
"facebook" : "https://facebook.com",
"twitter" : "https://twitter.com"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment