Golang ProgramsGolang Programs

This sample program demonstrates how to decode a JSON string.

Go language comes with more flexibility to work with the JSON document. In below example you can decode or unmarshal the JSON document into a map variable. The function Unmarshal of the json package is used to decode JSON values into Go values.
// This sample program demonstrates how to decode a JSON string.

package main

import (
	"encoding/json"	// Encoding and Decoding Package

// JSON Contains a sample String to unmarshal.

var JSON = `{
	"name":"Mark Taylor",
	"jobtitle":"Software Developer",
	"email":"[email protected]"

func main() {
	// Unmarshal the JSON string into info map variable.
	var info map[string]interface{}

	// Print the output from info map.
C:\golang>go run example36.go
Mark Taylor
Software Developer
[email protected]


Here is the syntax for Unmarshal functions in Go:

func Unmarshal(data []byte, variable interface{}) error

The function Unmarshal parses the JSON-encoded data and stores the result into the second argument ( variable interface{} ).

The map variable "info" is declared as a map with a key of type string and a value of type interface{}. This means the map can store any type of value for any given key.