Peter Hon
Peter Hon

Reputation: 331

How to handle JSON dynamic key in Go

If I have json like this:

 {"phonenumber": "3456789", emoji: {"emoji1": "12", "emoji2": "23", ...}

This is two level JSON, where the key value inside emoji will be generated dynamically, it means the key name is not fixed, and the number of key value pair will changed accordingly. So what is the syntax to marshal this JSON into a Go struct?

Upvotes: 0

Views: 1285

Answers (1)

Simon Fox
Simon Fox

Reputation: 6425

Use a map:

type Data struct {
    PhoneNumber string            `json:"phonenumber"`
    Emoji       map[string]string `json:"emoji"`
}

playground link

Upvotes: 2

Related Questions