how to convert json to map in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To convert json to map in golang, use json.Unmarshal() method with []byte, it will convert json string to map. You have to only pass your json string with byte in json.Unmarshal() method.


Related Tutorials

Golang JSON to string

Golang JSON to XML

Golang JSON to Map

Golang JSON to Struct


Today, I will show you how do I convert a json to map in golang, as above mentioned I’m going to use json.Unmarshal() method.

To use the json.Unmarshal() method, we have first import "encoding/json" package, then we have to create map variable which we will use to assign value.

Let’s start our Golang convert json to map example

main.go

package main
 
import (
    "encoding/json"
    "fmt"
)
 
func main() {
    jsonStr := `{"id": 1, "name": "aGuideHub"}`
    var x map[string]interface{}
 
    json.Unmarshal([]byte(jsonStr), &x)
    fmt.Println(x)
}

In the above example, we have converted the json value to a map and printed in golang console. let’s check the output.

Output

map[id:1 name:aGuideHub]

I hope it helps you, All the best 👍.

Try it Yourself

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts