Type problem in JSON conversion


I was wondering why when doing a Marshal and an Unmarshal the numbers are always taken as float64.

Here I put the replication of the error:

package main

import (

type abc struct {
	Name string
	Data interface{}

func main() {
	var idUser int64 = 1
	a := abc {
			"id_user": idUser, // string - int64
			"name": "a", // string - string
			"id_a": 1, // string - int
			"id_b": 1.2, // string - float
	b, _ := json.Marshal(a)
	// verify types
	var data abc
	_ = json.Unmarshal(b, &data)	
	values := data.Data.(map[string]interface{})	
	for k, v := range values{
		fmt.Printf("type key: %T, type value: %T\n", k, v)


Would it be a bug in the functions of the json package from go?

Thanks to everyone beforehand

As JSON does only know floats, that’s correct. If though you use typed JSON decoders, you can also read them as int or json.Number.

This probably goes without saying, but if you know your fields ahead of time, you can always provide type in the struct itself rather than using “interface{}”.

Something like this:

Hi @thenorthnate

It is with an external library. What I did was replicate the scenario.

Thanks for the reply.

1 Like

Hi @NobbZ

Thanks for the reply.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.