Go JSON debugging

Go json.Unmarshal Errors

Go’s encoding/json package separates malformed JSON from data that parses but cannot fit the destination Go type. Understanding that distinction makes json.Unmarshal failures much faster to diagnose.

Quick answer

A SyntaxError means the JSON text itself is malformed. An UnmarshalTypeError means the JSON is syntactically valid but a value cannot be assigned to the destination Go field type.

Syntax errors

Messages such as invalid character ... looking for beginning of value often point to non-JSON input or malformed syntax. Inspect the raw body, especially when consuming HTTP responses.

Type mismatches

If JSON sends a string where the struct expects an int, Unmarshal can return an UnmarshalTypeError. Compare the payload to the struct tags and expected field types.

Unknown fields

json.Unmarshal normally ignores unknown object fields. For stricter API validation, use a Decoder and call DisallowUnknownFields so unexpected properties become errors.

Strict Go decoder example

dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
if err := dec.Decode(&payload); err != nil {
    // inspect syntax/type/unknown-field error
}

Related JSON tools and guides

Frequently asked questions

Does json.Unmarshal reject unknown fields?

Not by default. Use Decoder.DisallowUnknownFields for strict object-field checking.

What is UnmarshalTypeError?

It indicates valid JSON contained a value that could not be assigned to the requested Go destination type.

How do I debug invalid character < in Go JSON?

Inspect the raw HTTP response; the server may have returned HTML instead of JSON.

Source-backed reference

JSON standards quick reference

Concise, standards-backed facts for developers working with JSON debugging and validation. Each claim is linked to the evidence registry and resolves to a primary standard or platform reference so it can be independently verified.

JSON standard
JSON is a text format for serializing structured data, with syntax defined by interoperable standards rather than by FixTools.
Strings and object keys
JSON strings and object member names use double quotation marks. Single-quoted strings are not valid standard JSON syntax.
Trailing commas
Standard JSON grammar does not allow a comma after the final member of an object or the final element of an array.
Parser behavior
JSON.parse() parses JSON text into a JavaScript value and raises a SyntaxError when the input is not valid JSON.

Common invalid → valid JSON examples

Trailing comma

{"a":1,}{"a":1}

Single-quoted key

{'a':1}{"a":1}

Unsupported literal

{"score":NaN}{"score":null}

Built for verification, not just extraction

Direct answers are paired with source-specific evidence. This keeps technical claims independently verifiable and gives search and answer systems a clearer provenance trail than unsupported summary copy.