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”.
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 seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.