# Type problem in JSON conversion

**URL:** <https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420>\
**Category:** Getting Help\
**Created:** [June 17, 2020, 5:32pm UTC](https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420 "2020-06-17T17:32:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andres\_Latorre](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/andres_latorre/32/7910_2.png) [@Andres\_Latorre](https://forum.golangbridge.org/u/Andres_Latorre)\
**Post date:** [June 17, 2020, 5:32pm UTC](https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420/1 "2020-06-17T17:32:11Z")

</div>

Hello,

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 (
	"fmt"
	"encoding/json"
)

type abc struct {
	Name string
	Data interface{}
}
			

func main() {
	var idUser int64 = 1
	a := abc {
		"Abc",
		map[string]interface{}{
			"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)
	}
}

```

[https://play.golang.org/p/Ax014b0JK3E](https://play.golang.org/p/Ax014b0JK3E)

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

Thanks to everyone beforehand

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [June 17, 2020, 6:05pm UTC](https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420/2 "2020-06-17T18:05:20Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![thenorthnate](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/thenorthnate/32/7969_2.png) [@thenorthnate](https://forum.golangbridge.org/u/thenorthnate)\
**Post date:** [June 17, 2020, 8:44pm UTC](https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420/3 "2020-06-17T20:44:53Z")

</div>

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:  
[https://play.golang.org/p/jlC2vsfX45V](https://play.golang.org/p/jlC2vsfX45V)

---

<div class="post-metadata">

**Author:** ![Andres\_Latorre](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/andres_latorre/32/7910_2.png) [@Andres\_Latorre](https://forum.golangbridge.org/u/Andres_Latorre)\
**Post date:** [June 17, 2020, 8:52pm UTC](https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420/4 "2020-06-17T20:52:54Z")

</div>

Hi @thenorthnate

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

Thanks for the reply.

---

<div class="post-metadata">

**Author:** ![Andres\_Latorre](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/andres_latorre/32/7910_2.png) [@Andres\_Latorre](https://forum.golangbridge.org/u/Andres_Latorre)\
**Post date:** [June 17, 2020, 8:53pm UTC](https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420/5 "2020-06-17T20:53:12Z")

</div>

Hi @NobbZ

Thanks for the reply.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [September 15, 2020, 8:53pm UTC](https://forum.golangbridge.org/t/type-problem-in-json-conversion/19420/6 "2020-09-15T20:53:14Z")

</div>

Closed on September 15, 2020, 8:53pm UTC

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