Welcome To Golang By Example

Convert a JSON to map in Go (Golang)

Table of Contents

Overview

encoding/json package provides utilities that can be used to convert to and from JSON. The same utility can be used to convert a golang map to JSON string and vice versa.

Examples

json.Unmarshal function can be used to convert a JSON to the map

There can be two cases

map[string]string
map[string]interface{}

Let’s see some examples. In the first example, we have below JSON string

{"1":"John"}

Assume that the format of the JSON is known. Below is the program

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	j := `{"1":"John"}`
	var b map[string]string
	json.Unmarshal([]byte(j), &b)

	fmt.Println(b)
}

Output

map[1:John]

In the second example, we have below JSON string

{"1":{"Name":"John"}}

We are parsing this JSON string into the below map type

map[int]employee

where the employee is a struct

type employee struct {
    Name string
}

This is the code

package main

import (
	"encoding/json"
	"fmt"
)

type employee struct {
	Name string
}

func main() {
	j := `{"1":{"Name":"John"}}`
	var b map[int]employee
	json.Unmarshal([]byte(j), &b)

	fmt.Println(b)
}

Output

map[1:{John}]

Let’s also see an example of the case where the format of the JSON is not known.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	j := `{"1":"John"}`
	var b map[string]interface{}
	json.Unmarshal([]byte(j), &b)

	fmt.Println(b["1"])
}

Output

John