Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

json-go

JSON encoding

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches JSON encoding: the syntax, patterns, and safety habits you need before advancing in Go.

Teams still ship JSON encoding in Go codebases—skipping it leaves gaps in debugging and code reviews.

You will apply JSON encoding in contexts like: Kubernetes ecosystem tools, cloud APIs, and CLI utilities.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

encoding/json marshals structs to JSON and unmarshals back—field tags control names: `json:"name"`. Essential for REST APIs and Kubernetes manifests.

Marshal and Unmarshal

type User struct {
    Name string `json:"name"`
}
b, _ := json.Marshal(User{Name: "Ada"})
var u User
json.Unmarshal(b, &u)

Exported fields only—unexported fields are ignored. Use json.RawMessage for deferred parsing.

Important interview questions and answers

  1. Q: Why pointer to Unmarshal target?
    A: Unmarshal must modify the value—pass &struct.
  2. Q: omitempty tag?
    A: Omits field when zero value—smaller API payloads.

Self-check

  1. What tag renames a JSON field?
  2. Why must JSON fields be exported?

Pitfall: Unexported struct fields are invisible to encoding/json—capitalize field names or use custom MarshalJSON.

Interview prep

Why pointer to Unmarshal?

Unmarshal must modify the destination value—pass address of struct.

json tag omitempty?

Omits field when zero value—smaller API payloads.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • struct tags json?
  • Marshal indent?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump