
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Parse JSON Files in Golang
Suppose we want to read a JSON file in Go. By reading, we mean that we want to convert the file data into a struct in Go.
Consider the JSON file shown below that we will use to read and then convert the data into the structure.
{ "users": [ { "name": "Mukul Latiyan", "social": { "facebook": "https://facebook.com/immukul", "twitter": "https://twitter.com/immukul", "linkedin": "https://linkedin.com/immukul" } }, { "name": "Mayank", "social": { "facebook": "https://facebook.com/mayank101", "twitter": "https://twitter.com/mayank101", "linkedin": "https://linkedin.com/mayank101" } }, { "name": "Deepak", "social": { "facebook": "https://facebook.com/deepak1", "twitter": "https://twitter.com/deepak1", "linkedin": "https://linkedin.com/deepak1" } } ] }
The above JSON file should be saved by the name users.json. It contains different users and each user has a name and different social fields.
A very naive approach to read the above JSON in Golang is to make use of the unstructured approach. In the unstructured approach, we use the interfaces{} to read the JSON data.
Consider the code shown below that will allow us to read the users.json file.
Example 1
package main import ( "encoding/json" "fmt" "io/ioutil" "log" "os" ) func main() { fileContent, err := os.Open("users.json") if err != nil { log.Fatal(err) return } fmt.Println("The File is opened successfully...") defer fileContent.Close() byteResult, _ := ioutil.ReadAll(fileContent) var res map[string]interface{} json.Unmarshal([]byte(byteResult), &res) fmt.Println(res["users"]) }
Output
If we run the command go run main.go on the above code, then we will get the following output in the terminal.
The File is opened successfully... [map[name:Mukul Latiyan social:map[facebook:https://facebook.com/immukul linkedin:https://linkedin.com/immukul twitter:https://twitter.com/immukul]] map[name:Mayank social:map[facebook:https://facebook.com/mayank101 linkedin:https://linkedin.com/mayank101 twitter:https://twitter.com/mayank101]] map[name:Deepak social:map[facebook:https://facebook.com/deepak1 linkedin:https://linkedin.com/deepak1 twitter:https://twitter.com/deepak1]]]
The next step is to create structs that will hold the JSON data. Consider the structs mentioned below.
type Users struct { Users []User `json:"users"` } type User struct { Name string `json:"name"` Social Social `json:"social"` } type Social struct { Facebook string `json:"facebook"` Twitter string `json:"twitter"` LinkedIn string `json:"linkedin"` }
The final step is to make use of the json.Unmarshal() function.
Example 2
package main import ( "encoding/json" "fmt" "io/ioutil" "log" "os" ) type Users struct { Users []User `json:"users"` } type User struct { Name string `json:"name"` Social Social `json:"social"` } type Social struct { Facebook string `json:"facebook"` Twitter string `json:"twitter"` LinkedIn string `json:"linkedin"` } func main() { fileContent, err := os.Open("users.json") if err != nil { log.Fatal(err) return } fmt.Println("The File is opened successfully...") defer fileContent.Close() byteResult, _ := ioutil.ReadAll(fileContent) var users Users json.Unmarshal(byteResult, &users) for i := 0; i < len(users.Users); i++ { fmt.Println("User Name: " + users.Users[i].Name) fmt.Println("Facebook Url: " + users.Users[i].Social.LinkedIn) } }
Output
If we run the command go run main.go on the above code, then we will get the following output in the terminal.
The File is opened successfully... User Name: Mukul Latiyan Facebook Url: https://linkedin.com/immukul User Name: Mayank Facebook Url: https://linkedin.com/mayank101 User Name: Deepak Facebook Url: https://linkedin.com/deepak1