Use map[string]any or json.RawMessage to parse JSON with unknown or changing structures in Go.
Use json.RawMessage or map[string]any to handle dynamic JSON structures without defining a fixed struct.
import "encoding/json"
data := map[string]any{}
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
// handle error
}
// Access dynamic fields safely
if val, ok := data["key"].(string); ok {
// use val
}
Handling dynamic or unknown JSON in Go lets your program read data where keys or structure might change unexpectedly. Instead of forcing the data into a rigid shape, you load it into a flexible container that adapts to whatever the JSON contains. It is like opening a mystery box and looking inside to see what you got, rather than trying to force the box into a specific shape before opening it.