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
How to 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