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) error

Parameters & Attributes

NameTypeDescription
dst*bytes.BufferDestination buffer; output is appended.
src[]byteInput JSON bytes.
prefixstringPrefix added to indented output lines.
indentstringIndentation 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())}
Go Reference