To lowercase JSON key names with JSON Marshal in Go, you can “add a json tag to each field in your struct, specifying the desired lowercase key name”.
In Golang, the encoding/json package marshals structs into JSON using the field names as JSON key names. By default, it uses the field names as they appear in the struct. However, you can control the JSON key names using the json tag with the omitempty option.
Example
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Age int `json:"age"`
}
func main() {
psn := Person{
FirstName: "Krunal",
LastName: "Lathiya",
Age: 30,
}
// Marshal the Person struct into JSON
jsonData, err := json.Marshal(psn)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return
}
// Print the JSON data
fmt.Println(string(jsonData))
}
Output
{"firstName":"Krunal","lastName":"Lathiya","age":30}
In this example, we defined a Person struct with FirstName, LastName, and Age fields.
We added the json tags to each field to specify the desired lowercase JSON key names.
When we marshal the Person struct into JSON using json.Marshal(), the resulting JSON object will have lowercase key names as specified in the json tags.
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.