how to convert json string to map in golang?
September 28, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
To convert json string to map in golang, just use json.Marshal([]byte[jsonString], &x)
method and pass your json string, it will convert json string into map.
Follow the below tutorial if you are struggling with installing GO in windows.
https://aguidehub.com/blog/how-to-install-golang-in-windows/
Today, I will show you how do I convert json string to map in golang, as above mentioned I’m going to use json.Marshal([]byte[jsonString], &x)
way.
The strings
package provide json.Marshal([]byte[jsonString], &x)
method.
Let’s start our Golang convert json string to map example
Convert whole json string into map example
main.go
package main
import (
"encoding/json"
"fmt"
)
func main() {
jsonStr := `{"domain-1":"aguidehub.com", "domain-2":"infinitbility.com", "domain-3": "sortoutcode.com"}`
x := map[string]string{}
json.Unmarshal([]byte(jsonStr), &x)
fmt.Println(x)
}
In the above example, we have converted json string to map and printed in golang console. let’s check the output.
Output
map[domain-1:aguidehub.com domain-2:infinitbility.com domain-3:sortoutcode.com]
I hope it helps you, All the best 👍.