I am working with JSON whose structure I may not always know. Unmarshalling the data into map[string]any
hence is the most appropriate for my use case. Now the issue is if there’s any numeric value in the JSON data, json.Unmarshal
converts it into float64
which is problematic because I may loose precision if the value is actually an integer. Thus when unmarshalling I want the value to be of type int64
in the map when possible but there doesn’t seem to be an easy to accomplish this. What should be my approach to solve this?
Question posted in Json
Our archive of expertly curated questions and answers provides insights and solutions to common problems related to this popular data interchange format. From parsing and manipulating JSON data to integrating it with various programming languages and web services, our archive has got you covered. Start exploring today and take your JSON skills to the next level
Our archive of expertly curated questions and answers provides insights and solutions to common problems related to this popular data interchange format. From parsing and manipulating JSON data to integrating it with various programming languages and web services, our archive has got you covered. Start exploring today and take your JSON skills to the next level
2
Answers
The answer you received in the comments is correct and general, but I will give you a more comprehensive answer with an example
One possible approach to solving this issue is to define a custom type that implements the
json.Unmarshaler
interface. This allows you to control the unmarshalling process and handle the JSON numeric values as integers when possible. Below is an example implementation:With this custom type, you can define your map as
map[string]JSONNumber
, and when unmarshalling, numeric values will be converted to either int64 or float64, depending on their nature. You can then access the values usingmap[key].AsInt64
ormap[key].AsFloat64
.Here’s an example of using this approach:
In this example, the value of
"key1"
is unmarshalled asint64
, while the value of"key2"
is unmarshalled asfloat64
.A
float64
is a 64-bit IEEE 754 double-precision type. The internal representation of this type exactly represents integers in the range -2^53 to 2^53, so you only need be concerned if the integers in your problem domain lie outside this range.That is: −9,007,199,254,740,992 to 9,007,199,254,740,992
You may be trying to solve a non-problem. 🙂