10 examples of 'golang unmarshal json to struct' in Go

Every line of 'golang unmarshal json to struct' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
41func JSONUnmarshal(data []byte, v interface{}) error {
42 return json.Unmarshal(data, v)
43}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

15func (xml *Parser) Unmarshal(p []byte, v interface{}) error {
16 res, err := x.Convert(bytes.NewReader(p))
17 if err != nil {
18 return fmt.Errorf("unmarshal xml: %w", err)
19 }
20
21 if err := json.Unmarshal(res.Bytes(), v); err != nil {
22 return fmt.Errorf("convert xml to json: %w", err)
23 }
24
25 return nil
26}
71func (c *protoCodec) UnmarshalStruct(data []byte, mold interface{}) error {
72 return c.structDecoder.Unmarshal(data, mold)
73}
633func (pbi *ProcBindInfo) UnmarshalJSON(data []byte) error {
634 err := defaultProcBindInfoHandle.UJSON(data, pbi)
635 if err != nil {
636 return err
637 }
638 return nil
639}
73func (m *yamlMarshaler) Unmarshal(data []byte, v interface{}) error {
74 // TODO: use a "strict" marshaler, so that we can warn on unrecognized keys (avoiding silly mistakes). We should
75 // set aside an officially sanctioned area in the metadata for extensibility by 3rd parties.
76 return yaml.Unmarshal(data, v)
77}
91func (et *EventType) UnmarshalJSON(data []byte) error {
92 switch string(data) {
93 case "\"added\"":
94 *et = EventAdded
95 case "\"removed\"":
96 *et = EventRemoved
97 default:
98 fmt.Println(string(data))
99 return errors.New("bad event type")
100 }
101
102 return nil
103}
598func (b *Boxer) unmarshal(data []byte, v interface{}) error {
599 mh := codec.MsgpackHandle{WriteExt: true}
600 dec := codec.NewDecoderBytes(data, &mh)
601 return dec.Decode(&v)
602}
45func (this *JSONArray) Unmarshal(obj interface{}, keys ...string) error {
46 return jsonUnmarshal(this, obj, keys)
47}
23func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error {
24 var m map[string]interface{}
25 if err := jsonx.Unmarshal(content, &m); err != nil {
26 return err
27 }
28
29 return unmarshaler.Unmarshal(m, v)
30}
322func (rp *rawPresentation) UnmarshalJSON(data []byte) error {
323 type Alias rawPresentation
324
325 alias := (*Alias)(rp)
326 rp.CustomFields = make(CustomFields)
327
328 err := unmarshalWithCustomFields(data, alias, rp.CustomFields)
329 if err != nil {
330 return err
331 }
332
333 return nil
334}

Related snippets