Standard library / encoding/json

encoding/json.Compact()

Remove insignificant JSON whitespace while preserving string contents; append to dst. Invalid syntax returns an error. Expected stdout: '{"x":"a b"}\n'.

Formal Syntax & Structure

go syntaxgo
func Compact(dst *bytes.Buffer, src []byte) error

Parameters & Attributes

NameTypeDescription
dst*bytes.BufferDestination buffer; output is appended.
src[]byteInput JSON bytes.

Return Value

error

Nil on success; otherwise an operation-specific error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/json"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  must(json.Compact(&b, []byte(` { "x" : "a b" } `)))  fmt.Println(b.String())}
Go Reference