encoding/json
Encodingimport "encoding/json"Implements encoding and decoding of JSON. Essential for APIs and configuration handling.
Example
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
data, _ := json.Marshal(User{"Alice", 30})
fmt.Println(string(data))
}Key Types & Functions
MarshalUnmarshalEncoderDecoderRawMessageNewEncoderAbout encoding/json
The encoding/json package (imported as encoding/json) belongs to the Encoding category of Go packages. Implements encoding and decoding of JSON. Essential for APIs and configuration handling.
Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The encoding/json package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.
When using encoding/json in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.