Home > front end >  json: cannot unmarshal string into Go value of type MyMap.map
json: cannot unmarshal string into Go value of type MyMap.map

Time:12-08

I met problem with marshaling JSON in golang.

I need to unmarshal json object, that I recieve from UDP packets. But I met problem with unmarshaling - it doesn't want to unmarshal properly.

I get "Error with unmarshaling: json: cannot unmarshal string into Go value of type main.MyMap" error. I tested in different ways, but feel stuck on this example - marshaland unmarshal in one line, and still get errors.

    package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type MyMap struct {
    Param map[string]string `json:"packet"`
}

func main() {
    rawJson := []byte(`{
        "packet":{
            "hostname":"host1",
            "pid":"123435",
            "processname":"process",
            "type":"partial"}
        }`)

    data, err := json.Marshal(rawJson)
    if err != nil {
        log.Println("Error with marchal JSON: "   err.Error())
    }

    var unmarshaled MyMap

    err = json.Unmarshal(data, &unmarshaled)
    if err != nil {
        fmt.Printf("Error with unmarshaling: %v", err)
        return
    }

    fmt.Printf("Read a message from %v     %s \n", unmarshaled.Param["pid"], unmarshaled.Param["processname"])
}

If I'm trying to unmarshal JSON, that I received from UDP, the error says

invalid character 'i/x01' looking for beginning of value

I believe I get this kind of error because of my misunderstanding of marshal system. I'd be appreciate if you help me Thanks!

CodePudding user response:

you should change rawjson to string type and change your order code same as:

package main

import (
    "encoding/json"
    "fmt"
)

type MyMap struct {
    Param map[string]string `json:"packet"`
}

func main() {
    rawJson := `{
        "packet":{
            "hostname":"host1",
            "pid":"123435",
            "processname":"process",
            "type":"partial"}
        }`

    struct_instance := MyMap{}
    if er := json.Unmarshal([]byte(rawJson), &struct_instance); er != nil {
        fmt.Println(er)
    }
    fmt.Println(struct_instance)

    json_as_byte, er := json.Marshal(struct_instance)
    if er != nil {
        fmt.Println(er)
    }

    fmt.Println(string(json_as_byte))

}


CodePudding user response:

I did few changes in your code and it's worked very well.

You can run it here: https://go.dev/play/p/jvw9MsVFbHt

type mp map[string]string
type MyMap struct {
    Param mp `json:"packet"`
}

func main() {
    rawJson := []byte(`{
        "packet":{
            "hostname":"host1",
            "pid":"123435",
            "processname":"process",
            "type":"partial"}
        }`)
    data, err := json.Marshal(rawJson)     //Not Required
    if err != nil {
        fmt.Println("Error with marchal JSON: "   err.Error())
    }
    fmt.Println("data ", data)

    var res MyMap

    json.Unmarshal(rawJson, &res)
    fmt.Printf("Read a message from %v     %s \n", res.Param["pid"], res.Param["processname"])
}
  • Related