Skip to content

encoding/json: support omitempty and omitzero at API level #72972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hcburca opened this issue Mar 20, 2025 · 3 comments
Closed

encoding/json: support omitempty and omitzero at API level #72972

hcburca opened this issue Mar 20, 2025 · 3 comments
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@hcburca
Copy link

hcburca commented Mar 20, 2025

Currently json marshalling and unmarshalling supports omitting empty or zero fields by tagging the fields in their struct.

However, we have cases when we don't control the struct, or when we have an opaque map[string]any to marshal.

It would be useful to be able to do json.Marshal(data, json.OmitEmptyOption, json.OmitZeroOption)

@cherrymui
Copy link
Member

cherrymui commented Mar 20, 2025

This would need a proposal. See http://go.dev/s/proposal . Thanks.

See also #71497, where the json v2 package is being developed and discussed. It does have some API level options. Perhaps comment there if you need more options.

cc @dsnet @ianlancetaylor

@cherrymui cherrymui added FeatureRequest Issues asking for a new feature that does not need a proposal. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Mar 20, 2025
@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Mar 20, 2025
@hcburca
Copy link
Author

hcburca commented Mar 20, 2025

Looks like OmitZeroStructFields from the json v2 package would solve this request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants