Unmarshal a structure dynamic

type UserMessage struct {
ID primitive.ObjectID json:"id,omitempty" bson:"_id,omitempty"
Data DataType json:"data" bson:"data"
}

type DataType struct{
Details []DetailsData json:"details" bson:"details"
}

type DetailsData struct{
Name string json:"name" bson:"name"
Value []interface{} json:"value" bson:"value"
}

Sample data
`{“id”:“78787871287182”,
“data”:{
“details”:
[
{“name”:“Person”,“value”:“TestName”},
{“name”:“PersonRoles”,“value”:[{“name”:“DEV”,“type”:“open”},{“name”:“UAT”,“Type”:“closed”}]}
]
}
}’

I tried unmarshal by setup the DetailsData with interface{} and array of interface{}. Interface{} fails with value is just a string and [] interface{} fails to unmarshal when value is not an array.

Implement the jons.Unmarshaler interface and define the parsing behavior of the DetailsData object.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.