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/json-formatter to verify excluded fields. Proper field exclusion protects sensitive data and controls API surface area in production applications.
Last updated: December 23, 2025
Previous
When should I use json.NewDecoder vs json.Unmarshal in Go?
Next
What is the fastest way to parse large JSON files in Go?
Related Questions
How do I use Go struct tags to rename JSON fields?
Learn how to use Go struct tags to rename JSON fields. Master JSON marshaling with proper field naming and omitempty.
Why is omitempty not working for my Go struct fields?
Learn why omitempty is not working in Go struct JSON tags. Understand zero values, pointers, and proper optional field handling.
When should I use json.NewDecoder vs json.Unmarshal in Go?
Learn when to use json.NewDecoder vs json.Unmarshal in Go. Understand streaming vs in-memory JSON parsing.
Still have questions?
Can't find the answer you're looking for? Please reach out to our support team.