Alwin Doss
Alwin Doss

Reputation: 972

Variable JSON structure mapping with Go struct type

I have a JSON that is as follows

{
  "Key1": "Value1",
  "Key2": "Value2",
  "Key3": {
    "InnerKey1": "InnerValue1",
    "InnerKey2": "InnerValue2",
    ...
  }
}

The problem I have is with the Key3 structure which contains a key value of variable length. It is possible that the client sends me another key. How do I create a struct for this in Go

Upvotes: 1

Views: 84

Answers (1)

apxp
apxp

Reputation: 5914

You can use json2go. For the variable part you could use a map

And you get:

type AutoGenerated struct {
    Key1 string `json:"Key1"`
    Key2 string `json:"Key2"`
    Key3 map[string]string `json:"Key3"`
}

Upvotes: 1

Related Questions