Partially JSON unmarshal on a map in Go

My websocket server will receive and unmount JSON data. This data will always be wrapped in an object with key / value pairs. The key string will act as the value identifier, telling the Go server what value it has. Knowing what type of value I can go to JSON, unmarshal the value in the correct type of structure.

Each json object can contain several key / value pairs.

JSON example:

{ "sendMsg":{"user":"ANisus","msg":"Trying to send a message"}, "say":"Hello" } 

Is there an easy way to use the "encoding/json" package for this?

 package main import ( "encoding/json" "fmt" ) // the struct for the value of a "sendMsg"-command type sendMsg struct { user string msg string } // The type for the value of a "say"-command type say string func main(){ data := []byte(`{"sendMsg":{"user":"ANisus","msg":"Trying to send a message"},"say":"Hello"}`) // This won't work because json.MapObject([]byte) doesn't exist objmap, err := json.MapObject(data) // This is what I wish the objmap to contain //var objmap = map[string][]byte { // "sendMsg": []byte(`{"user":"ANisus","msg":"Trying to send a message"}`), // "say": []byte(`"hello"`), //} fmt.Printf("%v", objmap) } 

Thanks for any suggestion / help!

+60
json go map
Jun 16 2018-12-12T00:
source share
2 answers

This can be achieved by Unmarshalling in map[string]*json.RawMessage .

 var objmap map[string]*json.RawMessage err := json.Unmarshal(data, &objmap) 

For further analysis of sendMsg you can do something like:

 var s sendMsg err = json.Unmarshal(*objmap["sendMsg"], &s) 

For say you can do the same and undo in a line:

 var str string err = json.Unmarshal(*objmap["say"], &str) 
+122
Jun 16 '12 at 21:15
source share
— -

In addition to Steven Weinberg's answer, I have since implemented a handy tool called iojson that helps to fill in data with an existing object easily, as well as encoding an existing object in a JSON string. Iojson middleware is also provided for working with other intermediaries. Additional examples can be found at https://github.com/junhsieh/iojson

Example:

 func main() { jsonStr := `{"Status":true,"ErrArr":[],"ObjArr":[{"Name":"My luxury car","ItemArr":[{"Name":"Bag"},{"Name":"Pen"}]}],"ObjMap":{}}` car := NewCar() i := iojson.NewIOJSON() if err := i.Decode(strings.NewReader(jsonStr)); err != nil { fmt.Printf("err: %s\n", err.Error()) } // populating data to a live car object. if v, err := i.GetObjFromArr(0, car); err != nil { fmt.Printf("err: %s\n", err.Error()) } else { fmt.Printf("car (original): %s\n", car.GetName()) fmt.Printf("car (returned): %s\n", v.(*Car).GetName()) for k, item := range car.ItemArr { fmt.Printf("ItemArr[%d] of car (original): %s\n", k, item.GetName()) } for k, item := range v.(*Car).ItemArr { fmt.Printf("ItemArr[%d] of car (returned): %s\n", k, item.GetName()) } } } 

Output Example:

 car (original): My luxury car car (returned): My luxury car ItemArr[0] of car (original): Bag ItemArr[1] of car (original): Pen ItemArr[0] of car (returned): Bag ItemArr[1] of car (returned): Pen 
0
Nov 03 '16 at 18:36
source share



All Articles