Has Json tag but not exported [duplicate]

You’re adding a JSON tag to a field that isn’t exported.

Struct fields must start with upper case letter (exported) for the JSON package to see their value.

struct A struct {
    // Unexported struct fields are invisible to the JSON package.
    // Export a field by starting it with an uppercase letter.
    unexported string

    // {"Exported": ""}
    Exported string

    // {"custom_name": ""}
    CustomName string `json:"custom_name"`

The underlying reason for this requirement is that the JSON package uses reflect to inspect struct fields. Since reflect doesn’t allow access to unexported struct fields, the JSON package can’t see their value.

Leave a Comment