Encoding and Decoding using json.Marshal and json.Unmarshal


Example

package main

import (
	"encoding/json"
	"fmt"
	"log"
)

func encodingEx(i interface{}) []byte {
	data, err := json.Marshal(i)
	if err != nil {
		log.Fatalf("failed to encode: %s", data)
	}
	return data
}

func decodingEx(data string) interface{} {
	var i interface{}
	err := json.Unmarshal([]byte(data), &i)
	if err != nil {
		log.Fatalf("failed to decode: %s", err)
	}
	return i
}

func main() {
	fmt.Printf("\n######## ENCODING USING json.Marshal ###########\n")
	fmt.Printf("\nEncoded %d to %s", 100, encodingEx(100))
	fmt.Printf("\nEncoded %f to %s", 1.52, encodingEx(1.52))
	fmt.Printf("\nEncoded %s to %s", "Australia Canada", encodingEx("Australia Canada"))
	
	strArray := [5]string{"A","B","C","D","E"}	
	encoded := encodingEx(strArray)
	fmt.Printf("\nEncoded %v to %s", strArray, encoded)
	
	
	fmt.Printf("\n\n######## DECODING USING json.Unmarshal ###########\n")
	fmt.Printf("\nDecoded %f from %s", decodingEx("1000"), "1000")
	fmt.Printf("\nDecoded %v from %s", decodingEx(`["Australia"]`), "Australia")
	fmt.Printf("\nDecoded %v from %s", decodingEx(`["A","B","C"]`), `["A","B","C"]`)		
}

Output

######## ENCODING USING json.Marshal ###########

Encoded 100 to 100
Encoded 1.520000 to 1.52
Encoded Australia Canada to "Australia Canada"
Encoded [A B C D E] to ["A","B","C","D","E"]

######## DECODING USING json.Unmarshal ###########

Decoded 1000.000000 from 1000
Decoded [Australia] from Australia
Decoded [A B C] from ["A","B","C"]
Most Helpful This Week