Skip to content

Can't unmarshal map[string]any #50

@jney

Description

@jney

code:

package main

import (
	"encoding/json"

	"github.com/rs/zerolog/log"
	"github.com/shamaton/msgpack/v2"
)

type foo struct {
	Parameters map[string]any `json:"parameters,omitempty" msgpack:"parameters,omitempty"`
}

func main() {
	b := []byte(`{"parameters":{"a":{"b":"foo","c":"bar","d":5.37}}}`)
	e := &foo{}
	err := json.Unmarshal(b, e)
	if err != nil {
		log.Fatal().Err(err).Msg("json unmarshal")
	}
	b, err = msgpack.Marshal(e)
	log.Debug().Err(err).Bytes("bytes", b).Msg("marshal")

	e = &foo{}
	err = msgpack.Unmarshal(b, e)
	log.Debug().Err(err).Interface("event", e).Msg("unmarshal")
}

output:

{"level":"debug","bytes":"\ufffd\ufffdparameters\ufffd\ufffda\ufffd\ufffdb\ufffdfoo\ufffdc\ufffdbar\ufffdd\ufffd@\u0015z\ufffdG\ufffd\u0014{","time":"2025-09-09T10:00:52+02:00","message":"marshal"}
{"level":"debug","event":"marshaling error: json: unsupported type: map[interface {}]interface {}","time":"2025-09-09T10:00:52+02:00","message":"unmarshal"}

Is there a way to unmarshal map[string]any?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions