DevBolt
Processed in your browser. Your data never leaves your device.

Go JSON Formatter & Validator

Format and validate JSON for your Go projects. Paste your JSON below to format it, then use the Go code examples to marshal and unmarshal it in your application. Everything runs in your browser — safe for production data.

← Back to tools

JSON Formatter & Validator

Format, validate, and minify JSON data instantly.

How to format JSON in Go

Go's encoding/json package provides json.MarshalIndent() for pretty printing: output, err := json.MarshalIndent(data, "", " "). To parse JSON into a struct: err := json.Unmarshal([]byte(jsonStr), &result). For compact JSON, use json.Marshal(). To format raw JSON bytes, use json.Indent(): var buf bytes.Buffer; json.Indent(&buf, rawJSON, "", " "). Go's json package uses struct tags to map JSON keys: type User struct { Name string `json:"name"` }.

Common Go JSON patterns

For dynamic JSON without predefined structs, use map[string]interface{} or json.RawMessage. Go 1.18+ generics don't directly help with JSON, but packages like go-json and sonic offer faster marshaling. For streaming large JSON, use json.NewDecoder(reader) and json.NewEncoder(writer). Handle missing fields with pointer types (*string) or the omitempty tag. Custom marshaling: implement the json.Marshaler interface with MarshalJSON() ([]byte, error).

Why validate JSON before json.Unmarshal()

Go's json.Unmarshal returns generic errors that don't always pinpoint the exact issue. Using this formatter first, you can identify syntax errors (missing commas, unclosed brackets, trailing commas) before writing Go code. This is especially useful when debugging HTTP response bodies from http.Get() or API clients, testing Go struct tag mappings against real payloads, and validating configuration files used with json.NewDecoder().

Frequently Asked Questions

How do I pretty print JSON in Go?

Use json.MarshalIndent(data, "", " ") for struct data. For raw JSON bytes, use json.Indent(&buf, rawBytes, "", " ") with a bytes.Buffer. Both produce formatted JSON with 2-space indentation.

How do I handle unknown JSON fields in Go?

Use map[string]interface{} to unmarshal arbitrary JSON: var result map[string]interface{}; json.Unmarshal(data, &result). For partial parsing, embed json.RawMessage in your struct to defer decoding of specific fields.

Why does json.Marshal escape HTML characters?

Go's json.Marshal escapes <, >, and & to \u003c, \u003e, \u0026 for safe HTML embedding. To disable this, use json.NewEncoder with SetEscapeHTML(false): enc := json.NewEncoder(&buf); enc.SetEscapeHTML(false); enc.Encode(data).

Related Format Tools