Yet another go library for common json operations. The purpose of this library is to assist Go developers in fast track json handling and save them from hassle of writing repeated code.


Handler interface is the core of this library, which exposes a number of different methods to handle json.

type Handler interface {
 Display(msg interface{})
 StringToStruct(s string, i interface{}) error
 StructToString(i interface{}) (string, error)
 StringToMap(s string) (map[string]interface{}, error)
 MapToString(m map[string]interface{}) (string, error)
 BytesToString(jsonBytes []byte) string
 StringToBytes(s string) []byte
 StructToBytes(i interface{}) (jsonBytes []byte, err error)
 BytesToStruct(b []byte, d interface{}) error
 ModifyInputJson(s string) (map[string]interface{}, error)
 DisplayJsonFieldTypes(jsonBytes []byte) error
 TriggerAllJsonMethods(str string)

Vet whole project by,

go vet ./...

run all tests in json_handler_test.go by,

go test ./...

How to use

import json-common-utilities-go into the user module by including following lines in go.mod file

require (
    github.com/azam-akram/json-common-utilities-go v0.0.6

Have a look at TriggerAllJsonMethods(str string) to know how to use other functions exposed by JsonHandler interface. If you want to see how to call all or any of these functions, simply look at TriggerAllJsonMethods(str string)

Example: A user main function calling TriggerAllJsonMethods(str string) which shows how to call all the functions exposed by Handler interface.

package main

import (

 json_handler "github.com/azam-akram/json-common-utilities-go/json-handler"

var employeeStr = string(`{
    "id": "The ID",
    "name": "The User",
    "designation": "CEO",
            "doorNumber": 1,
            "street": "The office street",
            "city": "The office city",
            "country": "The office country"
            "doorNumber": 1,
            "street": "The home street",
            "city": "The home city",
            "country": "The home country"

func main() {
 fmt.Println("Starting main..")


func TriggerAllJsonMethods(employeeStr string) {
 jh := json_handler.NewJsonHandler()

 var emp *model.Employee
 _ = jh.StringToStruct(employeeStr, &emp)
 logger.GetLogger().PrintKeyValue("StringToStruct: ", "emp", emp)

 outputStr, _ := jh.StructToString(&emp)
 logger.GetLogger().PrintKeyValue("StructToString: ", "str", outputStr)

 jMap, _ := jh.StringToMap(outputStr)
 id := jMap["id"].(string)
 user := jMap["name"].(string)
 logger.GetLogger().PrintKeyValue("StringToMap: ", "id", id, "user", user)

 mapData := map[string]interface{}{
  "id":   "The ID",
  "name": "The User",

 outputStr, _ = jh.MapToString(mapData)
 logger.GetLogger().PrintKeyValue("MapToString: ", "str", outputStr)

 inputBytes := []byte(`{"id": "The ID", "name": "The User"}`)
 outputString := jh.BytesToString(inputBytes)
 logger.GetLogger().PrintKeyValue("BytesToString: ", "outputString", outputString)

 outputBytes := jh.StringToBytes(outputString)
 logger.GetLogger().PrintKeyValue("StringToBytes: ", "outputBytes", outputBytes)

 outputBytes, _ = jh.StructToBytes(emp)
 logger.GetLogger().PrintKeyValue("StructToBytes: ", "outputBytes", outputBytes)

 var newEmp *model.Employee
 _ = jh.BytesToStruct(outputBytes, &newEmp)
 logger.GetLogger().PrintKeyValue("BytesToStruct: ", "newEmp", newEmp)

 logger.GetLogger().PrintKeyValue("Before calling ModifyInputJson: ", "employeeStr", employeeStr)
 _ = jh.StringToStruct(outputStr, &emp)
 logger.GetLogger().PrintKeyValue("Before calling ModifyInputJson: ", "emp", emp)
 modifiedEmp, _ := jh.ModifyInputJson(employeeStr)
 logger.GetLogger().PrintKeyValue("After calling ModifyInputJson: ", "modifiedEmp", modifiedEmp)


View Github