-3

How do I make the key name Id lower case in the marshaled JSON output for this code?

    type Topic struct {
        Id string
    }

    topics := []Topic{
        {Id: "some identifier"},
        {Id: "some other identifier"},
    }

    tops, err := json.Marshal(topics)
    if err != nil {
        fmt.Println("got an error", err)
    }

    fmt.Println(string(tops))

Returns:

[
    {"Id":"some identifier"},
    {"Id":"some other identifier"}
]

But the API I'm using requires lower case, like:

[
    {"id":"some identifier"},
    {"id":"some other identifier"}
]

I am still pretty new to golang, so any direction is appreciated!

Dshiz
  • 3,099
  • 3
  • 26
  • 53

1 Answers1

2

You just set the json struct tag

type Topic struct {
    Id string `json:"id"`
}
dave
  • 62,300
  • 5
  • 72
  • 93
  • You know, just as you answered, that very thought occurred to me, and I was about to delete the question. – Dshiz Oct 05 '21 at 18:36