Go
/
Updated 02 Sep 2025
/
3 min read
Compact JSON is efficient for transport, but long documents can be difficult to inspect when everything appears on one line. Go’s standard encoding/json package includes json.MarshalIndent for producing human-readable JSON with line breaks and indentation.
Pretty-printed JSON is useful for debugging, generated configuration, logs intended for people, and command-line output.
1. Use json.MarshalIndent package main import ( "encoding/json" "fmt" ) func main() { data := map[string]any{ "name": "Alice", "age": 30, "address": map[string]string{ "street": "123 Main St", "city": "Wonderland", }, "hobbies": []string{"reading", "hiking", "coding"}, } prettyJSON, err := json.MarshalIndent(data, "", " ") if err != nil { fmt.Println("Error:", err) return } fmt.Println(string(prettyJSON)) } MarshalIndent accepts three arguments: