JSON Tags
Control marshaling.
JSON Tags is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
JSON Tags Control Marshaling
The encoding/json package uses json tags to decide the key names and rules when converting structs to and from JSON.
Default Field Names
Without tags, JSON keys match the exported field names exactly, including capitalization.
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string
Age int
}
func main() {
b, _ := json.Marshal(User{"Ann", 30})
fmt.Println(string(b))
}Renaming Keys
A json tag changes the key name in the output. Here Name becomes the lowercase name.
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string "json:\"name\""
Age int "json:\"age\""
}
func main() {
b, _ := json.Marshal(User{"Ann", 30})
fmt.Println(string(b))
}The omitempty Option
Add ,omitempty to skip a field in the output when it holds its zero value (empty string, 0, nil, etc).
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string "json:\"name\""
Email string "json:\"email,omitempty\""
}
func main() {
b, _ := json.Marshal(User{Name: "Ann"})
fmt.Println(string(b))
}Ignoring a Field
A tag of - tells the encoder to skip the field completely, useful for secrets like passwords.
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string "json:\"name\""
Password string "json:\"-\""
}
func main() {
b, _ := json.Marshal(User{"Ann", "secret"})
fmt.Println(string(b))
}Unmarshaling with Tags
Tags work both ways. When decoding JSON, the tag tells the decoder which JSON key fills which field.
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string "json:\"name\""
}
func main() {
var u User
json.Unmarshal([]byte("{\"name\":\"Bob\"}"), &u)
fmt.Println(u.Name)
}Case-Insensitive Matching
When unmarshaling, Go matches JSON keys to tags case-insensitively as a fallback. Still, defining tags explicitly is the safe, clear choice.
String Option
The ,string option encodes a numeric or bool field as a JSON string. Useful for systems that expect numbers quoted.
package main
import (
"encoding/json"
"fmt"
)
type Item struct {
ID int "json:\"id,string\""
}
func main() {
b, _ := json.Marshal(Item{ID: 42})
fmt.Println(string(b))
}Combining Options
You can combine a name with options: json:"email,omitempty". The name comes first, then comma-separated options.
Unexported Fields Skipped
Only exported (capitalized) fields are marshaled. A lowercase field is invisible to encoding/json no matter what tag it has.
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string "json:\"name\""
age int
}
func main() {
b, _ := json.Marshal(User{Name: "Ann", age: 30})
fmt.Println(string(b))
}Pretty Output
json.MarshalIndent formats JSON with indentation, handy for readable logs while still honoring tags.
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string "json:\"name\""
}
func main() {
b, _ := json.MarshalIndent(User{"Ann"}, "", " ")
fmt.Println(string(b))
}Quick Check
What does the ,omitempty option do in a json tag?
Recap
JSON tags control marshaling:
- Rename keys with
json:"name" ,omitemptyskips zero values-ignores a field;,stringquotes numbers- Only exported fields are encoded
Frequently asked questions
Is the “JSON Tags” lesson free?
Yes — the full text of “JSON Tags” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “JSON Tags”?
Control marshaling. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “JSON Tags” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.