Golang encoding/json.Compact() function example

package encoding/json

Compact appends to dst(1st parameter) the JSON-encoded src (2nd parameter) with insignificant space characters elided.

Golang encoding/json.Compact() function usage example

 package main

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

 func main() {
 dst := new(bytes.Buffer)

 src := []byte(`{
 "Name":"Adam Ng",
 "Age":36,
 "Job":"CEO"
 }`)

 err := json.Compact(dst, src)

 if err != nil {
 fmt.Println(err)
 }

 fmt.Println(dst)
 }

Output :

{"Name":"Adam Ng","Age":36,"Job":"CEO"}

Reference :

http://golang.org/pkg/encoding/json/#Compact

Advertisement