Go JSON

How can I ignore specific fields during JSON marshaling in Go?

Ignore struct fields during JSON marshaling using the dash tag: type User struct { Password string `json:"-"` } excludes Password from JSON output completely. This is essential for sensitive data or internal-only fields. For conditional exclusion based on values, use omitempty: `json:"field,omitempty"` omits zero values. Unexported fields (lowercase) are automatically excluded since json package cannot access them. For complex logic, implement custom MarshalJSON() method on your type: func (u User) MarshalJSON() ([]byte, error) { /* custom logic */ }. Use anonymous struct literals to selectively include fields: json.Marshal(struct{ Name string }{Name: user.Name}). Create separate DTO structs for different contexts needing different field visibility. Embedding with tags controls inherited field behavior. Test marshaling output with our JSON Formatter at jsonconsole.com to verify excluded fields. Proper field exclusion protects sensitive data and controls API surface area in production applications.
Last updated: December 23, 2025

Still have questions?

Can't find the answer you're looking for? Please reach out to our support team.