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) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| dst | *bytes.Buffer | Destination buffer; output is appended. |
| src | []byte | Input 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())}