Skip to content

Instantly share code, notes, and snippets.

@NaniteFactory
Last active July 17, 2018 11:28
Show Gist options
  • Select an option

  • Save NaniteFactory/8ebddabedb3688840cb6b332ad3fdece to your computer and use it in GitHub Desktop.

Select an option

Save NaniteFactory/8ebddabedb3688840cb6b332ad3fdece to your computer and use it in GitHub Desktop.
Parse & convert JSON into a map
package main
import (
"encoding/json"
"fmt"
)
func main() {
data := []byte(`{"sendMsg":{"user":"ANisus","msg":"Trying to send a message"},"say":"Hello"}`)
fmt.Println("-----------------")
var map_canonical interface{}
err = json.Unmarshal(data, &map_canonical)
fmt.Println(err, map_canonical)
fmt.Println("-----------------")
map_string_string := map[string]string{}
json.Unmarshal(data, &map_string_string)
for k, v := range map_string_string {
fmt.Println(k + ":" + v)
}
fmt.Println("-----------------")
map_string_rawjson := map[string]json.RawMessage{}
json.Unmarshal(data, &map_string_rawjson)
var submap map[string]string
json.Unmarshal(map_string_rawjson["sendMsg"], &submap)
for k, v := range submap {
fmt.Println(k + ":" + v)
}
fmt.Println("-----------------")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment