Why JSON silently changes your big numbers (and how to stop it)

Try it in the tool

JSON Formatter

Beautify, validate and minify JSON with precise error locations.

JSON Validator

Check JSON syntax and spot duplicate keys and inexact numbers.

JSON to TypeScript

Generate clean TypeScript interfaces from any JSON sample.

You paste an API response into a tool, format it, and everything looks fine. Then a lookup fails because the id in your output is not the id the server sent. No error, no warning. This is one of the most common silent data bugs in web development, and it comes from one detail: a JavaScript number is a 64-bit float.

See it happen

Take this document:

{"id": 9007199254740993, "price": 0.1, "note": "id is odd"}

The id ends in 3, so it is odd. Now parse it in a browser console or in Node.js:

JSON.parse('{"id": 9007199254740993}')
// { id: 9007199254740992 }

The 3 became a 2. The program did not complain, because rounding to the nearest representable value is exactly what floating-point numbers are supposed to do. Run the same text through the JSON formatter and the output still says 9007199254740993, because the tool never converts the number into a float. It keeps the digits you gave it.

Where the precision goes

A 64-bit float spends 52 bits on the fraction, which gives 53 bits of precision. Every integer up to 2^53 (9007199254740992) can be stored exactly. Number.MAX_SAFE_INTEGER is one less, 9007199254740991. Above that, the spacing between representable numbers grows to 2, then 4, then 8. The odd integer 9007199254740993 sits exactly between two neighbours, and the rule picks the even one.

Two consequences matter in practice:

  • Different IDs can collapse into one value, so a map or a Set of IDs silently loses entries.
  • The error is invisible when you read the data, because the rounded number prints normally.

Decimals have a related, more familiar problem: 0.1 + 0.2 is 0.30000000000000004 in every language that uses binary floats. For money, send integers (cents) or strings, not fractions.

Which values are at risk

The danger is not “big numbers” in general. It is identifiers and counters that outgrow 2^53:

  • Snowflake-style IDs used by social networks and chat platforms, 64-bit integers by design.
  • Database BIGINT keys from sequences that have run for years.
  • Nanosecond timestamps: the current Unix time in nanoseconds is around 1.7 × 10^18, far above 2^53.
  • Hashes or counters stored as integers.

A response with a 19-digit number is a warning sign. Open it in the JSON validator first: it reports numbers outside the safe range, so you know before the data reaches code.

Fixes that work

  1. Send big IDs as strings. {"id": "9007199254740993"} survives every parser. This is the fix to prefer when you control the API, and it is why many APIs return a string copy of each numeric ID.
  2. Parse with a library that keeps big integers. In JavaScript, use a parser that returns BigInt or the original text for numbers outside the safe range. The built-in reviver argument of JSON.parse cannot fix this on its own: the number is already rounded before the reviver sees it. Only the newest engines pass the original source text as an extra argument.
  3. In Go, decode with json.Number. Decoding into interface{} produces float64 by default, which has the same problem. Decoder.UseNumber() keeps the digits.
  4. Do not round-trip data through JSON.parse and JSON.stringify in scripts that only need to reformat. Use a formatter that works on the text.
  5. Generate types that say what the field is. When you convert a sample response to types with JSON to TypeScript, a 19-digit field is a hint to declare it string or bigint yourself instead of number.

Finding the problem in your own data

Rounding leaves no trace in the output, so you have to look for it on purpose. Three habits catch it early:

  • Compare digits, not values. Take an ID from the raw response (curl, the network panel, a log line) and compare it character by character with what your program holds. If the last digits differ, something parsed it as a float.
  • Look at the length. Any integer with 16 or more digits is suspect. A 19-digit ID is almost certainly a 64-bit key.
  • Test with a number just above the limit. Put 9007199254740993 in a fixture. If a test that round-trips it fails, you found the weak link before a customer did.

A quick checklist

  • Does any field exceed 9007199254740991? If yes, treat it as an identifier and transport it as a string.
  • Does any consumer run in a browser or Node.js? Then it will round silently.
  • Did a tool in the middle (a proxy, a log viewer, a formatter) parse and re-print the body? Compare its output with the raw text.

Everything on this page runs the way the tools do: in your browser, with nothing uploaded. Paste the sample document into the formatter and compare it with what your own code prints.

Frequently asked questions

Is this a bug in JSON?

No. JSON is only text: the digits 9007199254740993 are a perfectly valid number. The loss happens when a program reads that text into a 64-bit floating-point number, which is what every JavaScript number is. RFC 8259 even warns that numbers outside the range and precision of IEEE 754 doubles may not be interoperable.

Which numbers are safe in JavaScript?

Integers between -9007199254740991 and 9007199254740991 (Number.MAX_SAFE_INTEGER). Past that, some integers have no exact representation and are rounded to a neighbour, so two different IDs can become the same value.

Should I send big IDs as strings?

Yes, when the consumer might be JavaScript. A quoted ID survives every parser unchanged. Twitter's API became the best-known example: it returns both id (a number) and id_str (a string) for this reason.

Does a JSON formatter change my numbers?

A formatter built on JSON.parse followed by JSON.stringify does. The formatter on this site reads numbers as text and prints the original digits, so formatting never rewrites a value.

More guides