Last Updated: May 22, 2026
The default JSON output that Go produces from a struct doesn't always match the format your API or storage layer expects. Money stored as cents needs to wire out as "$24.99", an enum value needs to read as "shipped" instead of 2, and a calendar date needs to land on disk as "2024-05-16" without a time component. The encoding/json package lets any type take full control of its own JSON representation by implementing the Marshaler and Unmarshaler interfaces, and there's a lighter TextMarshaler interface that works across JSON, XML, CSV, and environment variables at once.