How to make JSON Marshal in Go output keys in lowercase?
Hi everyone! I'm writing my first REST API in Go and ran into a problem: when serializing a struct to JSON via `json.Marshal`, all fields are output with capital letters because exported fields in Go start with a capital letter. For example:
```go
type User struct {
Name string
Email string
Age int
}
func main() {
u := User{"Ivan", "ivan@example.com", 30}
data, _ := json.Marshal(u)
fmt.Println(string(data))
// Output: {"Name":"Ivan","Email":"ivan@example.com","Age":30}
}
```
But on the frontend (React), everyone expects keys in camelCase or snake_case: `name`, `email`, `age`. I tried writing my own marshaller, but that's too cumbersome for a project with 20+ structs.
I've read about `json:"name"` tags, but manually writing them for each field — that's
No answers yet.
Log in to leave an answer