Serde

24 articles
Fix: "json: cannot unmarshal X into Go value of type Y" Fix the JSON unmarshal error by ensuring your Go struct field names and types exactly match the incoming JSON data structure. Fix: "json: unsupported type" in Go Fix the 'json: unsupported type' error by removing functions, channels, or complex interfaces from your struct before marshaling to JSON. How to Convert Between JSON and Structs Using Code Generators Go uses the encoding/json package with struct tags to manually map JSON data to structs instead of generating code. How to Handle Dynamic or Unknown JSON in Go Use map[string]any or json.RawMessage to parse JSON with unknown or changing structures in Go. How to Handle JSON Dates and Times in Go Handle JSON dates in Go tar archives by using Header time fields and setting Format to PAX or GNU for precision. How to Handle JSON with Nested Objects in Go Unmarshal nested JSON in Go by defining a struct with sub-struct fields and using json.Unmarshal. How to Handle omitempty with Structs, Pointers, and Zero Values The omitempty tag skips fields only when they are their zero value, requiring pointers for structs to omit based on specific internal states. How to Handle Optional/Nullable JSON Fields in Go Use pointer types (e.g., `*string`, `*int`) for struct fields to distinguish between a missing JSON key and a zero-value field, or use `json.RawMessage` for dynamic handling. How to Marshal (Encode) JSON in Go Use the `encoding/json` package's `Marshal` function to convert Go data structures into JSON byte slices, or `MarshalIndent` if you need pretty-printed output with indentation. How to Marshal/Unmarshal Enums in JSON in Go Go enums marshal to JSON as their underlying integer or string values automatically without custom code. How to Parse XML in Go with encoding/xml Parse XML in Go by defining a struct with xml tags and calling encoding/xml.Unmarshal on the raw data. How to Pretty-Print JSON in Go Use the `encoding/json` package's `MarshalIndent` function to format JSON with indentation and newlines, or pipe the output through the `jq` command-line tool for quick terminal formatting. How to Read and Write YAML in Go (gopkg.in/yaml.v3) Use yaml.Marshal to write Go structs to YAML and yaml.Unmarshal to read YAML back into Go structs. How to Stream JSON with json.Decoder and json.Encoder Stream JSON data in Go using json.Decoder and json.Encoder to process large payloads sequentially without loading them entirely into memory. How to Unmarshal (Decode) JSON in Go Decode JSON data into Go variables using the json.Unmarshal function from the encoding/json package. How to Unmarshal JSON into a map[string]interface{} in Go Decode JSON bytes into a flexible map[string]interface{} using json.Unmarshal. How to Use Custom JSON Marshaling and Unmarshaling in Go Implement MarshalJSON and UnmarshalJSON methods on your type to override default JSON encoding and decoding behavior in Go. How to Use encoding/csv for CSV Files in Go Use the encoding/csv package to easily read and write CSV files in Go with built-in parsing and formatting. How to Use encoding/gob for Go-Specific Serialization Serialize Go structs to binary streams using encoding/gob's Encoder and Decoder for efficient data persistence and transmission. How to Use json.RawMessage for Deferred Decoding Use json.RawMessage to store JSON as raw bytes and defer parsing until the data is explicitly accessed. How to Use MessagePack in Go Encode and decode Go structs to binary MessagePack format using the vmihailenco/msgpack library for efficient data serialization. How to Use Protocol Buffers (Protobuf) in Go Generate Go code from .proto files using the protoc compiler and the Go plugin to handle data serialization. How to Use Struct Tags for JSON in Go Use backtick-delimited struct tags with the json key to rename fields, omit empty values, or exclude data during JSON marshaling. How to Validate JSON in Go Validate JSON in Go by using json.Unmarshal and checking the returned error to ensure the data is well-formed.