How to map a field in a Go struct to camelCased JSON when using json.Marshal
.
In this example, the struct field Number
is output as someNumber
, and Title
is aTitle
.
package main
import (
"encoding/json"
"fmt"
)
type Foo struct {
Number int `json:"someNumber"` // <---- this is called a "json hint"
Title string `json:"aTitle"`
}
func main() {
foo_marshalled, _ := json.Marshal(Foo{Number: 1, Title: "test"})
fmt.Printf("%v\n", string(foo_marshalled))
}
go run foo.go
{"someNumber":1,"aTitle":"test"