Created
April 26, 2017 14:06
-
-
Save sathishvj/846965ab8a77b34d34b8e625c990e638 to your computer and use it in GitHub Desktop.
Custom json decoding for changing one member data field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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