Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Tuesday, April 11, 2023

Parsing JSON Data using Golang

 

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.

No comments:

Post a Comment