The number that does not fit — int64, bigint and decimal
A JSON number above 9007199254740992 is already wrong by the time a generator sees it. Here is why, and which type to ask for instead.
Take this JSON:
{ "id": 9007199254740993, "balance": 10.10 }
Open this example in the tool →
Paste it into almost any generator, ask for TypeScript, and you get:
export interface Root {
id: number
balance: number
}
Both of those are wrong, in two different ways, and neither will tell you until money or identity is involved.
Why the id is already lost
JSON says a number is a number. It does not say how big, and it does not say
how precise. Nearly every JSON reader turns a number into a double, the
same 64-bit floating point value that 1.5 uses.
A double holds whole numbers exactly up to 9007199254740992, which is
2 to the power of 53. Above that, it starts skipping. The value
9007199254740993 cannot be held, so it becomes 9007199254740992. No error,
no warning: a different number.
Run this in any browser console:
JSON.parse('{"id":9007199254740993}').id
// 9007199254740992
This matters because the numbers that go over that line are exactly the ones you must not get wrong: a Twitter or X id, a Snowflake id, a Discord id, a bank account number, a transaction reference, a nanosecond timestamp.
And here is the part that catches generators: the tool reads your sample with the same JSON reader. The value is wrong before the generator ever sees it. It cannot warn you about a digit it never received.
Why the balance is wrong too
10.10 is not a problem of size. It is a problem of base. A double is binary,
and 0.1 has no exact binary form, the way one third has no exact decimal
form. So:
0.1 + 0.2
// 0.30000000000000004
For money, that is not acceptable. Add enough of them and the total is off by a cent, and a cent that nobody can explain is worse than a cent that is missing.
What to ask for instead
For a big whole number
| Language | Ask for | Note |
|---|---|---|
| TypeScript | string, or bigint |
JSON.parse cannot make a bigint. You need a reviver, or keep it as text |
| Python | int |
Python integers have no limit. You are safe already |
| Go | int64, or json.Number |
int64 holds it. The default float64 does not |
| Rust | i64 or u64 |
serde handles these correctly |
| Java | long, or BigInteger |
Jackson gives a long when the value needs one |
| C# | long |
The same |
| Swift | Int |
64-bit on every current device |
TypeScript is the awkward one, and the honest answer is: keep the id as a
string. If you own the API, send it as a string. Many large APIs do exactly
that, and this is why. If you do not own it, read the body as text and pull
the id out before JSON.parse touches it.
For money
Never a double. Use the decimal type of your language, or count in the smallest unit:
| Language | Type |
|---|---|
| Python | Decimal |
| Java | BigDecimal |
| C# | decimal |
| Go | shopspring/decimal, or an int64 of cents |
| TypeScript | a string, parsed into a decimal library, or an integer of cents |
| Rust | rust_decimal |
The integer-of-cents answer is the simplest one that always works: store
1010, not 10.10, and divide only when you print.
How to see it before it bites
Our JSON Viewer reads a document without calling
JSON.parse. It keeps the exact characters of every number from the source
text, so a value too big for a double is shown with all its digits and the
viewer can say so. That is the one place you can see the true value before a
reader destroys it.
In your own code, the check is this: take the number as text, parse it, print it again, and compare the two strings. If they differ, the value did not survive.
What we will do about it
This tool will warn you when a sample holds a number that a double cannot
hold, and offer the right type for the language you picked, rather than
number. Until then, the rule is simple:
If an id is longer than 15 digits, make it a string. If it is money, make it a decimal.
Read next
The JSON Viewer has a longer guide on why big numbers break in JSON, with the arithmetic drawn out.
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to CodeRead next
Dates in JSON, and the type a generator should give you
JSON has no date. What you get depends on the format in your sample, and an epoch number is almost always missed.
snake_case to camelCase — what a generator renames, and why
Your JSON keys and your language's naming rules disagree. Here is what gets renamed, what keeps the original name, and where it goes wrong.
When one field holds two types
An array of mixed things, a field that is a string on Monday and a number on Tuesday. What a generator does, and what you should do instead.