Go
Updated September 19, 2026
Format JSON in Go
Pretty-print JSON with predictable indentation and validation in Go.
Formatting JSON means parsing valid JSON and serializing it again with whitespace and indentation that make the structure easier to read. Parsing first also catches malformed input.
Quick Start#
The following example uses the standard Go API or a widely used package for this task.
Quick start#
Go
Common Operations#
This second pattern covers a common production operation or a safer way to handle real input.
Compact or indent raw JSON#
Go
Important Notes#
json.Indent and json.Compact operate on raw JSON bytes, which avoids converting numbers into float64 through interface{}.
Best Practices#
- Treat external input as untrusted and handle parse/validation errors explicitly.
- Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
- Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.