I have a JSON in the following format:
{
"fieldA": {
"dynamicField": "string value",
"moreDynamicField": ["or", "array", "of", "strings"],
"allFieldsAreDynamic": "values in str or arr[str] only"
},
"fieldB": {
"dynamicField": "sameAsThoseInFieldA",
}
}
The outer fields are fixed, but each outer field's value is a dynamic object. Therefore, I attempted to unmarshall this data into the following Go struct:
type JsonData struct {
fieldA map[string]interface{}
fieldB map[string]interface{}
}
func main() {
var data JsonData
json.Unmarshal([]byte(`{"fieldA": {"x":"x", "y":["x","y"]}, "fieldB": {"a":"b", "c":"d"}}`), &data)
fmt.Println(data)
}
However, the above code produces empty Go array with no error: {map[] map[]}
. I also tried fieldA map[string]map[string]any
but the result is the same.
I didn't spot useful information on the json package documentation about this nested unmarshalling. Plus, will there be a performance drop using pure map[string]interface{}
for the entire JSON data? Some benchmarking blogs and reddit tales gives contradictory conclusions...