I am trying to deserialize JSON which looks like this:
{
"pattern": {"@odata.type": "microsoft.graph.recurrencePattern"},
"range": {"@odata.type": "microsoft.graph.recurrenceRange"}
}
For this I created several structs where the first one looks like this:
type MSPatternedRecurrence struct {
Pattern MSRecurrencePattern `json:"@odata.type"`
Range MSRecurrenceRange `json:"@odata.type"`
}
However go vet throws an error like this:
struct field Range repeats json tag "@odata.type"
What is the right way to create a struct in this situation?
type MSPatternedRecurrence struct { Pattern MSRecurrencePattern json:"@odata.type"
Range MSRecurrenceRange json:"@odata.type"
}
type MSRecurrencePattern struct {
DayOfMonth int `json:"dayOfMonth"`
DayOfWeek []string `json:"daysOfWeek"`
FirstDayOfWeek string `json:"firstDayOfWeek"`
Index string `json:"index"`
Interval int `json:"interval"`
Month int `json:"month"`
Type string `json:"type"`
}
type MSRecurrenceRange struct {
EndDate string `json:"endDate"`
NumberOfOccurrences int `json:"numberOfOccurrences"`
RecurrenceTimeZone string `json:"recurrenceTimeZone"`
StartDate string `json:"startDate"`
Type string `json:"type"`
}
No; the error clearly states you're trying to map two struct fields to the same JSON field name, which you cannot do; also, the definitions for the types used for those fields aren't given, but it seems unlikely they're correct given that the JSON has them both as simple strings.
You have two fields, pattern
and range
. Each's value is an object. Those objects each have a field named @odata.type
. I.e.:
type Odata struct {
Type string `json:"@odata.type"`
}
type MSPatternedRecurrence struct {
Pattern Odata
Range Odata
}
You might find the JSON-to-Go tool useful. For this JSON, it outputs:
type AutoGenerated struct {
Pattern struct {
OdataType string `json:"@odata.type"`
} `json:"pattern"`
Range struct {
OdataType string `json:"@odata.type"`
} `json:"range"`
}