Skip to content

Instantly share code, notes, and snippets.

@darcwader
Created August 18, 2016 06:57
Show Gist options
  • Save darcwader/fa83a0b538242f65a40b07f49e0184ed to your computer and use it in GitHub Desktop.
Save darcwader/fa83a0b538242f65a40b07f49e0184ed to your computer and use it in GitHub Desktop.
golang keeping additional json fields as is
package main
import (
"encoding/json"
"fmt"
)
func main() {
str := `{"hello":{"nested":{"world":"!"}}}`
fmt.Println("input string : ", str)
s := struct {
Hello map[string]interface{}
}{}
err := json.Unmarshal([]byte(str), &s)
if err != nil {
panic(err)
}
fmt.Println("struct : ", s)
b, err := json.Marshal(s)
fmt.Println("marshalled back(notice cap hello): ", string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment