Standard library / encoding/json
encoding/json.Indent()
Append indented JSON to a buffer, prefixing each new line. Leading whitespace is discarded, trailing whitespace is preserved. Expected stdout: '{\n "x": 1\n}\n'.
Formal Syntax & Structure
go syntaxgo
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| dst | *bytes.Buffer | Destination buffer; output is appended. |
| src | []byte | Input JSON bytes. |
| prefix | string | Prefix added to indented output lines. |
| indent | string | Indentation string for each nesting level. |
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.Indent(&b, []byte(`{"x":1}`), "", " ")) fmt.Println(b.String())}