Hello everyone, The golang has a lot of notable features to support developers to develop an application, and performing the marshalling and unmarshalling is a most common and frequent activity for any software application during the development. In this tutorial, we will show you how to perform a json marshalling and unmarshalling in golang. This tutorial has step by step approach and the example program, which has been tested and shared in the same post.

How to perform a JSON Marshalling and Unmarshalling in Golang
How to Perform JSON Marshalling and Unmarshalling in Golang

What is Marshalling and Unmarshalling?

The marshalling is a process of converting an object(type) into a JSON string or file. But, Unmarshalling is a reverse process of marshalling, which means the given json string or file will be converted to the desired object(type).

#1 Creating a type in golang

In order to perform the json marshalling and unmarshalling in golang, first of all, you need a model (i.e. type) in golang. The below snippet will give you an idea about how to create a type in golang.

// creating a type in golang
type Employee struct {
Id int
Name string
Age int
}

In golang, the type can be created using “type” and “struct” keywords, inside the type you can add an attribute with it`s data-type.

#2 JSON Marshalling in golang

Once, the type(model) is created, we can pass the same to perform json marshalling in golang by using json.Marshal() function.

// function to perform marshalling
func doMarshall(emp Employee) {
data, err := json.Marshal(emp)
if err != nil {
fmt.Println("Error!") // error message
}
os.Stdout.Write(data)
}

#3 JSON Unmarshalling in golang

The JSON unmarshalling in golang is quite easier to perform, In order to do that, you simply need to pass the JSON string with the type that needs to be converted. The unmarshalling can be done using json.unmarshalling() function in golang.

// function to perform unmarshalling
func doUnMarshall(jsonString string) {
var emp Employee;
err := json.Unmarshal([]byte(jsonString), &emp)
if err != nil {
fmt.Println("Error!") // error message
}
fmt.Println("\n", emp)
}

#4 Full program(JSON marshalling and unmarshalling in golang)

I hope now you understand how to perform a marshalling and unmarshalling in golang, please find the complete program below to use it as part of your application.

package main
import (
"encoding/json"
"fmt"
"os"
)
// creating a type in golang
type Employee struct {
Id int
Name string
Age int
}
func main() {
// assigning values to Employee type
e := Employee {
Id: 1,
Name: "Dinesh Krishnan",
Age: 20,
}
// performing JSON marshalling
doMarshall(e)
// performing JSON unmarshalling
doUnMarshall("{\"Id\":1,\"Name\":\"Dinesh Krishnan\",\"Age\":20}")
}
// function to perform marshalling
func doMarshall(emp Employee) {
data, err := json.Marshal(emp)
if err != nil {
fmt.Println("Error!") // error message
}
os.Stdout.Write(data)
}
// function to perform unmarshalling
func doUnMarshall(jsonString string) {
var emp Employee;
err := json.Unmarshal([]byte(jsonString), &emp)
if err != nil {
fmt.Println("Error!") // error message
}
fmt.Println("\n", emp)
}

Output

{"Id":1,"Name":"Dinesh Krishnan","Age":20}
{1 Dinesh Krishnan 20}

References

  1. https://golang.org/doc/
  2. https://golang.org/pkg/
  3. https://golang.org/pkg/fmt/
  4. https://golang.org/pkg/fmt/#Println
  5. https://golang.org/pkg/encoding/json/

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *