Working with JSON in Golang

Kushagra Kesav
Published in
2 min readJun 23, 2022


Image by Author

Golang provides multiple APIs to work with JSON including to and from built-in and custom data types using the encoding/json package.

Golang converts the object into JSON and JSON data to string with Marshal() and Unmarshal() methods. The methods returned data in byte format and we need to change returned data into JSON or String.

In this article, we will understand data marshaling and unmarshalling in Golang.

Marshal in Golang

Converting Go objects into JSON is called marshaling in Golang using inbuilt encoding/json package to perform JSON-related operations.

json.Marshal(v any) ([]byte, error)

With the help of the above function, Marshal returns the JSON encoding of v and further, we can convert the byte data into a string and return it.

Unmarshal in Golang

It’s just the opposite of Marshal. Converting JSON(Byte data) into Struct and stores the result in the value pointed to by the variable.

json.Unmarshal(data []byte, v any)

With the help of the above function, Unmarshal returns the actual value of the JSON data.

Now let’s look use both the above function to write some code and do some modifications to the JSON dataset.

Let us consider the given data set: 👇🏻

"_id": "62b435e407bc586da8d297cb",
"index": 4,
"guid": "57a22da3-d791-4420-9245-465a34730537",
"gender": "male",
"company": "EZENT",
"email": "",
"phone": "+1 (801) 499-2594",
"friends": [
"id": 0,
"name": "Dale Sexton",
"email": ""
"id": 1,
"name": "Watkins Puckett",
"email": ""
"id": 2,
"name": "Luann Bond",
"email": ""
"greeting": "Hello, Barrera Jarvis! You have 8 unread messages.",
"favoriteFruit": "banana"

And, problem statement is to write down the code to delete the email field from the array of friends.

Now, let’s write down the code to modify the JSON data:

I hope you liked the article.

Thanks for Reading

Linkedin | Twitter