Developer Documentation
Go
Updated September 19, 2026

Validate JSON in Go

Validate JSON syntax and handle parser errors without mutating the input in Go.

Syntax validation answers one question: can the input be parsed as JSON? It is different from JSON Schema validation, which checks the shape and constraints of already valid JSON data.

Quick Start#

The following example uses the standard Go API or a widely used package for this task.

Syntax validation#

Go
1if json.Valid([]byte(input)) {2    fmt.Println("Valid JSON")3} else {4    fmt.Println("Invalid JSON")5}

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Decode with error details#

Go
1var value any2dec := json.NewDecoder(strings.NewReader(input))3if err := dec.Decode(&value); err != nil {4    return fmt.Errorf("invalid JSON: %w", err)5}6if dec.More() {7    return errors.New("unexpected additional JSON value")8}

Important Notes#

json.Valid is a fast syntax check. Use Decoder when you need decoded data or more detailed errors and stream handling.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement JSON Validator in other languages: