json.SyntaxError
GoERRORNotableJSON

invalid character … looking for …

Quick Answer

Type-assert to *json.SyntaxError to access the Offset and log the problematic position in the input.

What this means

Returned by json.Unmarshal or json.Decoder.Decode when the input is not valid JSON. The Offset field indicates where parsing failed.

Why it happens
  1. 1Malformed JSON input with missing quotes, brackets, or commas
  2. 2Unexpected BOM or non-UTF-8 bytes at the start of the payload

Fix

Type-assert and report offset

Type-assert and report offset
var se *json.SyntaxError
if errors.As(err, &se) {
    log.Printf("syntax error at offset %d", se.Offset)
}

Why this works

errors.As extracts the concrete type so the Offset field is accessible for diagnostics.

Code examples
Detectgo
var v any
err := json.Unmarshal([]byte("{bad}"), &v)
var se *json.SyntaxError
if errors.As(err, &se) {
    fmt.Println("offset:", se.Offset)
}
Validate input firstgo
if !json.Valid(data) {
    return fmt.Errorf("invalid JSON")
}
Streaming decodego
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
err := dec.Decode(&v)
Sources
Official documentation ↗

Go standard library

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Go errors