6. map string interface{}
map[string]interface{}
var data map[string]interface{}
jsonString := `{"name": "Alice", "age": 30, "address": {"city": "Wonderland"}}`
json.Unmarshal([]byte(jsonString), &data)
fmt.Println(data["name"]) // Output: Alice
fmt.Println(data["address"].(map[string]interface{})["city"]) // Output: WonderlandGo JSON - map[string]interface{} Notes
The Problem
// Sometimes:
{"type": "user", "name": "Alice"}
// Other times:
{"type": "org", "company": "Acme", "employees": 50}The Solution: map[string]interface{}
Breaking Down the Type
Why This Works for JSON
Example Usage
Accessing Nested Values
Type Assertions Explained
any is the Same as interface{}
any is the Same as interface{}Complete Example
Safe Type Assertion (Check for Errors)
When to Use map[string]interface{}
Structs vs map[string]interface{}
With Struct (better when structure is known)
With map[string]interface{} (when structure unknown)
JSON Number Gotcha
Common Patterns
Check if Key Exists
Iterate Over Keys
Nested Access with Safety
Key Takeaways
Quick Reference
JSON Type
Go Type in interface{}
Assignment
1
2