JSON (Javascript Object Notation) is a standard format for storing data. The JSON format is the most used format in web applications to read and write dynamic data. Most of RESTful APIs are implemented to return JSON data response.
As in our previous tutorial you have learned how to Read File Line by Line using Golang. Here in this tutorial we are going to explain how to parse JSON data using Golang to implement in your web applications.
We will cover this tutorial step by step to create a JSON data file and read JSON file data. The we handle functionality to parse JSON.
Also, read:
Step1: Create JSON Data File
First we will create a simple JSON file employee.json
using below JSON data to read the file and then parse it.
{ "employee": [ { "name": "Smith", "gender": "Male", "age": 40 }, { "name": "Rosy", "gender": "Female", "age": 30 } ] }
Step2: Read JSON Data File
To open JSON file, we will use os
package. After opening the file, we handle JSON data parsing and close the file at the end of function.
jsonFile, err := os.Open("employee.json") if err != nil { fmt.Println(err) } fmt.Println("Successfully Opened json file") defer jsonFile.Close()
Step3: Parse JSON Data with Structs
Now we will parse the employee JSON data with by defining structs. Then we will unmarshal the JSON data using a set of predefined structs.
package main import ( "encoding/json" ) type Employees struct { Employees []Employee `json:"employee"` } type Employee struct { Name string `json:"name"` Gender string `json:"gender"` Age int `json:"Age"` }
Step4: Unmarshalling JSON Data
After opening and reading the employee.json
file, we will convert file to byte array using ioutil.ReadAll()
method. Then we will pass byte array to json.Unmarshal()
method to unmarshall json data.
func main() { jsonFile, err := os.Open("employee.json") if err != nil { fmt.Println(err) } fmt.Println("Successfully Opened json file") defer jsonFile.Close() byteEmpValue, _ := ioutil.ReadAll(jsonFile) var employees Employees json.Unmarshal(byteEmpValue, &employees) for i := 0; i < len(employees.Employees); i++ { fmt.Println("Employee Name: " + employees.Employees[i].Name) fmt.Println("Employee Gender: " + employees.Employees[i].Gender) fmt.Println("Employee Age: " + strconv.Itoa(employees.Employees[i].Age)) } }
Step5: Complete Code to Parse JSON Data with Go
Here is the complete code main.go
file to parse JSON data using Golang.
package main import ( "encoding/json" "fmt" "io/ioutil" "os" "strconv" ) type Employees struct { Employees []Employee `json:"employee"` } type Employee struct { Name string `json:"name"` Gender string `json:"gender"` Age int `json:"Age"` } func main() { jsonFile, err := os.Open("employee.json") if err != nil { fmt.Println(err) } fmt.Println("Successfully Opened json file") defer jsonFile.Close() byteEmpValue, _ := ioutil.ReadAll(jsonFile) var employees Employees json.Unmarshal(byteEmpValue, &employees) for i := 0; i < len(employees.Employees); i++ { fmt.Println("Employee Name: " + employees.Employees[i].Name) fmt.Println("Employee Gender: " + employees.Employees[i].Gender) fmt.Println("Employee Age: " + strconv.Itoa(employees.Employees[i].Age)) } }
Conclusion
Hopefully this tutorial helped you to handle JSON data file parsing in Golang. If you find this tutorial helpful then please let us know in the comments section below.
You may also like:
- Regular Expressions in Golang
- Creating REST API with Golang
- How to Delete Files in Golang
- Working with Structs in Golang
- Working with Date and Time in Golang
- How to Make HTTP Requests in Golang
- Working with Channels in Golang
- Concurrency with Goroutines in Golang
- Working with Maps in Golang
- Marshalling and Unmarshalling in Golang
- AWS S3 File Upload in Golang
- Write Data to CSV File using Golang
- How to Read CSV File using Golang
- Read File Line by Line using Golang
Hi this is a very useful, simple explanation here. Thanks
You’re welcome, thanks!