Parsing into a struct is very convenient but sometimes you don’t know the structure of JSON document upfront.
For arbitrary JSON documents we can decode into a map[string]interface{}
, which can represent all valid JSON documents.
https://codeeval.dev/gist/d6c8c4a63a57fb264e105b568bbf176d
For basic JSON types, the value in the map is bool
, int
, float64
or string
.
For JSON arrays, the value is []interface{}
.
For JSON dictionaries, the value is (again) map[string]interface{}
.
This approach is flexible but dealing with map[string]interface{}
to access values is rather painful.