Skip to main content

How to Name JSON Fields with Struct Tags in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Go field names start with a capital letter, but JSON keys are usually lowercase. A struct tag, a short string after the field's type, tells encoding/json which key to use and whether to leave the field out when it's empty.

Syntax​

Syntax
type <Name> struct {
<Field> <type> `json:"<key>"`
<Field> <type> `json:"<key>,omitempty"`
}
PartWhat it does
`json:"<key>"`Uses <key> as this field's name in JSON.
,omitemptyLeaves the field out when its value is empty, such as "" or 0.
`json:"-"`Never encodes this field.

The encoding/json documentation: "The encoding of each struct field can be customized by the format string stored under the "json" key in the struct field's tag." And: "Each exported struct field becomes a member of the object".

The Example​

The example encodes a User with a name, an empty email and an unexported age, to show all three rules at once.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Struct and Encode It​

Create a file named main.go:

main.go, which declares a User struct whose Name field has the tag json:"name", whose Email field has json:"email,omitempty" and whose age field is lowercase, and prints json.Marshal of a user named Ada aged 36.

main.go
package main

import (
"encoding/json"
"fmt"
)

type User struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
age int
}

func main() {
data, err := json.Marshal(User{Name: "Ada", age: 36})
if err != nil {
fmt.Println("Could not encode:", err)
return
}
fmt.Println(string(data))
}

Step 2: Run It​

Terminal
go run main.go
Output
{"name":"Ada"}

A zsh terminal where go run main.go prints the JSON object with only the key name set to Ada.

  • Name became "name", as its tag says.
  • Email was empty, so omitempty left it out.
  • age starts with a lowercase letter, so it's unexported and encoding/json never sees it.

The command is the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
A space after the colon, as in `json: "name"`The tag isn't recognized, so the field keeps its Go nameWrite `json:"name"` with no spaces
Lowercase field namesThey're silently left out of the JSONCapitalize them and name the key with a tag
Expecting omitempty to keep a deliberate 00 counts as empty and is left outDrop omitempty for fields where 0 matters

Troubleshooting​

SymptomLikely causeFix
A field is missing from the JSONIt's unexported, empty with omitempty, or tagged json:"-"Check the field's name and tag
A key keeps its capital letterThe tag is malformedRun go vet: for `json: "name"` it reported struct field tag `json: "name"` not compatible with reflect.StructTag.Get: bad syntax for struct tag value

FAQ​

Do tags work when decoding too?​

Yes. json.Unmarshal uses the same tags to match JSON keys to fields.

Can a field have tags for several formats?​

Yes, separated by spaces: `json:"name" xml:"name"`.

Conclusion​

A tag like `json:"name,omitempty"` sets a field's JSON key and leaves it out when empty. Only exported fields are encoded, so capitalize fields and name their keys in tags.

Additional Resources​