Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Created April 26, 2017 14:06
Show Gist options
  • Save sathishvj/846965ab8a77b34d34b8e625c990e638 to your computer and use it in GitHub Desktop.
Save sathishvj/846965ab8a77b34d34b8e625c990e638 to your computer and use it in GitHub Desktop.
Custom json decoding for changing one member data field
package main
import (
"fmt"
"encoding/json"
)
var d1 = []byte(`
{
"ABC": {
"Val": "hello"
}
}
`)
var d2 = []byte(`
{
"DEF": {
"Val": "world"
}
}
`)
type Inside struct {
Val string
}
func main() {
var objMap map[string]*json.RawMessage
err := json.Unmarshal(d2, &objMap)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(objMap)
var in Inside
for k := range objMap {
err = json.Unmarshal([]byte(*objMap[k]), &in)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(in)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment