Back to course

JSON: Encoding and Decoding

Go (Golang) for Cloud-Native Microservices

Working with JSON

Microservices communicate via JSON. Go's encoding/json package makes this easy.

Decoding (Parsing JSON):

go var u User err := json.NewDecoder(r.Body).Decode(&u)

Encoding (Sending JSON):

go json.NewEncoder(w).Encode(response)

Always use Encoder/Decoder for streams (like HTTP requests) and Marshal/Unmarshal for strings/bytes in memory.