Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Marshal and unMarshal of Struct to JSON in Golang

Marshal And UnMarshal Of Struct To JSON In Golang

This Golang tutorial help to understand marshalling and un-marshalling of data using Golang. We can encode and decode Struct data using marshal and unmarshal.

This golang tutorial Convert STRUCT data into JSON and from JSON to string using marshaling and unmarshalling. The marshal and Unmarshal method returned in Bytes format, but we can change these data to strings/JSON in Go.

JSON is a language-independent data format.The golang is providing 'encoding/json' package for json related operation, It has many inbuilt method to process json data. The JSON parsing and generating JSON data is easily available in many programming languages.

In single line :”Marshal use to convert Go object into JSON and Unmarshal is vice versa.”

Marshalling in GoLang

json.Marshal() method help to convert Struct into Byte data, This method takes object as a param and returned Bytes code.We will create simple employee struct, that have name, age and salary property.

type Employee struct {
    Name string
    Age int
    salary int
}

We will convert Employee struct data into json using json.Marshal() method.The below code use to convert Struct data into Byte code using golang marshaling.

emp_obj := Employee{Name:"Rachel", Age:24, Salary :344444}
emp, _ := json.Marshal(emp_obj)
fmt.Println(string(emp))

Line 1: Creating object using Employee Struct.
Line 2: Convert Struct Object into Byte data.
Line 3: Convert Byte data into json string to display data.

The Output :
{"Name":"Rachel","Age":24,"Salary":344444}

Un-marshalling in GOLANG

json.Unmarshal() method help to convert json(Byte data) into Struct Object, This method takes json byte data as a param and returned struct object.I am using same above json string and convert into byte data, Finally convert json byte data into Employee struct.The Employee struct have Name, Age and Salary property.The string keys in the JSON are matched to the field names in the structs.

{"Name":"Rachel","Age":24,"Salary":344444}

We will convert above json string byte data and convert into Employee struct using json.Unmarshal() method.

We will create Response Struct that will use to match byte code using unmarshal.

type Response struct {
    Name string `json:"name"`
    Age int `json:"age"`
    Salary int `json:"salary"`
}

The below code use to convert Json Byte code data into GO Object.

bytes := []byte(str_emp)
var res Response
json.Unmarshal(bytes, &res)

fmt.Println(res.Name)

The Output :
Rachel

Line 1: Creating json string into byte code.
Line 2: Create empty Response struct and assign res variable.
Line 3: Unmarshal by passing a pointer to an empty structs.
Line 3: Print the Struct Name value.

The post Marshal and unMarshal of Struct to JSON in Golang appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

Marshal and unMarshal of Struct to JSON in Golang

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×