-5

I can't decode the m into values even though it's a nested map and I provided struct filed of type map. I don't understand why ?

most of the problems was in the struct fields I didn't capitalize it's names so they were not exported

    id  int //it should be Id  
    values map[interface{}]interface{}//should be Values 

and the map keys shouldn't be of type interface it should be of type string

Values map[interface{}]interface{}
//should be Values map[string]interface{} 

the filed name didn't match the json keys you can solve this problem using the struct tags or by matching them with json keys

//the key for the embedded object was m .
// was trying to decode it into struct 
//field and it's name was m .it was like this before fixing it  
type edit struct{
  Id int 
  Values [interface{}]interface{}
}
//and json object was like this 
{
  "id":5,
  "m": {"name":"alice"}
}
package main

import (
    "encoding/json"
    "fmt"
    "log"
    "strings"
)
type edit struct {
    Id  int 
    Values map[string]interface{}
 
 
}
func main() {
    a := `{
        "id":5,
        "values":{
        "name":"alice"
        }
}`
    var s edit
    dec := json.NewDecoder(strings.NewReader(a))
    err := dec.Decode(&s)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(s)
}

Play good code

abdelwhab
  • 3
  • 2
  • 2
    There are two issues. (1) Export the fields. See [json.Marshal(struct) returns “{}”](https://stackoverflow.com/a/26327436/5728991) for more info. (2) The structure of the JSON should match the structure of the Go types. Rename field `values` to `m` to match the JSON. – Charlie Tumahai Sep 02 '20 at 01:46
  • 2
    You can't json unmarshal into a map using `interface{}` as a key type. Also, `id` is a string not an `int`. Fix: https://play.golang.org/p/bXaUyQzDmAi – colm.anseo Sep 02 '20 at 02:53
  • Does this answer your question? [json.Unmarshal returning blank structure](https://stackoverflow.com/questions/28228393/json-unmarshal-returning-blank-structure) – Peter Sep 02 '20 at 07:48

1 Answers1

0

There are several issues in your code.

  1. First of all, all the fields of your edit struct must be exported which means you should capitalize the first letter of every fields.
    Just like the following:

    type edit struct {
        Id
        Values
    }
    
  2. The data type of of key of the Values field must be string to be able to decode the json data.

    type edit struct {
        Id     int
        Values map[string]interface{}
    }
    
  3. Finally, data type of your json data "id" is wrong. It must be int to be matched to your Go struct. Also, there is no "m" defined in your struct. The "m" must be replaced with "values" to match your Go struct. Just like the following

    {
        "id": 5,
        "values":{
            "name": "alice"
        }
    }
    



Just to let you know, you can simply use following piece of code to unmarshal json data into Go struct.

err := json.Unmarshal([]byte(a), &s)
Masudur Rahman
  • 1,585
  • 8
  • 16