I\'ve created an API in Go that, upon being called, performs a query, creates an instance of a struct, and then encodes that struct as JSON before sending back to the caller
Other answers already explained that this isn't possible using the encoding/json
package features directly.
Instead, I'd like to offer an alternative solution using a third-party library. Disclaimer: I am the author of the library.
https://github.com/wI2L/jettison
Jettison is a faster and flexible alternative to encoding/json, while still retaining a behavior identical to the standard library by default.
You can use the DenyList
option to filter-out fields that should be omitted from the result. Note that the feature is limited, and only works for 1st-level fields.