JSON Cannot Access Unexported Struct Fields

json: struct field name has no exported fields (silent failure — field is always empty)

Quick Answer

The struct field starts with a lowercase letter so json.Marshal and Unmarshal cannot see it. Capitalize the field name and use json tags.

Why This Happens

The encoding/json package uses reflection and can only access exported (capitalized) fields. Unexported fields are silently ignored during both marshaling and unmarshaling. This is a common source of confusion because there is no error, just empty or missing values.

The Problem

type User struct {
    name  string `json:"name"`  // unexported, ignored by json
    email string `json:"email"` // unexported, ignored by json
}

The Fix

type User struct {
    Name  string `json:"name"`
    Email string `json:"email"`
}

Step-by-Step Fix

  1. 1

    Identify unexported fields

    Check if the struct fields that are missing from JSON output start with a lowercase letter.

  2. 2

    Capitalize the fields

    Change the first letter of each field to uppercase to export it.

  3. 3

    Add json tags

    Use struct tags like `json:"fieldName"` to control the JSON key names.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free