0PricingLogin
Go Academy · Lesson

JSON Tags

Control marshaling.

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))
}

All lessons in this course

  1. What Are Struct Tags
  2. JSON Tags
  3. Validation Libraries
  4. Custom Tag Parsing
← Back to Go Academy