DEV Community

Maria BIBI
Maria BIBI

Posted on

JSON.parse Is Silently Changing Your IDs (and How to Stop It)

Here is a bug that throws no error, passes most tests, and quietly corrupts data:

const text = '{"id": 1849302746519283746, "name": "Emily"}'

JSON.parse(text).id
// 1849302746519283700
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

The last three digits changed from 746 to 700. No exception, no warning. If that id is a tweet, a Discord message, an order or a database row, your code now points at a record that does not exist, or worse, at someone else's.

I ran into this while building a JSON formatter, so I tested exactly where it happens and what actually fixes it. Every snippet below was run in Node.js 24 or Python 3.13.

Why it happens

JSON itself has no limit on number size. JavaScript does: every number is a 64-bit float, which can only represent integers exactly up to 2^53 - 1.

Number.MAX_SAFE_INTEGER        // 9007199254740991
2 ** 53 + 1                    // 9007199254740992  (not ...993!)
9007199254740993 === 9007199254740992   // true
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

Above that limit, JSON.parse picks the nearest number it can store. A 19-digit ID is far above it.

The same happens with response.json() in fetch, because it uses the same parser:

const res = await fetch('/api/order')   // body: {"id": 1849302746519283746}
const data = await res.json()
data.id   // 1849302746519283700
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

Long decimals lose digits too: 0.1234567890123456789 comes back as 0.12345678901234568.

Where these numbers come from

  • Snowflake IDs: Twitter/X, Discord, Instagram and many internal systems generate 64-bit IDs. That is why the Twitter API added an id_str field and the Discord API sends IDs as strings.
  • Database keys: a PostgreSQL bigint goes up to 9,223,372,036,854,775,807, about a thousand times the safe limit.
  • Money and science: long decimals in prices, coordinates or measurements.

Other languages are often fine. Python keeps big integers exact:

import json
json.loads('{"id": 1849302746519283746}')["id"]   # 1849302746519283746
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

So a Python backend can send correct JSON that only breaks when it reaches JavaScript, which makes the bug hard to spot.

Fix 1: send big IDs as strings (best)

If you control the API, this is the simplest and most robust fix:

{ "id": "1849302746519283746", "name": "Emily" }
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

IDs are labels, not quantities. You never add two order IDs, so a string loses nothing.

Fix 2: read the original text with context.source

Modern JavaScript lets a JSON.parse reviver see the exact source text of each value:

const data = JSON.parse(text, (key, value, context) =>
  key === 'id' ? BigInt(context.source) : value
)

data.id   // 1849302746519283746n  (a BigInt, every digit kept)
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

This works in Node.js 22 and later (I tested on Node 24) and in recent Chrome and Firefox. Check browser support before you rely on it in front-end code.

Fix 3: write it back with JSON.rawJSON

BigInt has a catch: JSON.stringify refuses it.

JSON.stringify({ id: 10n })
// TypeError: Do not know how to serialize a BigInt
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

The companion feature JSON.rawJSON writes a number exactly as you give it:

JSON.stringify({ ...data, id: JSON.rawJSON(data.id.toString()) })
// {"id":1849302746519283746,"name":"Emily"}
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

Fix 4: in Python, decimals need parse_float

Python's integers are safe, but floats are not:

from decimal import Decimal
json.loads('{"price": 0.1234567890123456789}', parse_float=Decimal)["price"]
# Decimal('0.1234567890123456789')
ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

How to check your own data

A quick test: run your JSON through JSON.parse and JSON.stringify and compare. If any number changed, you have the problem. Look for integers with 16 or more digits.

If you just want to look at or clean up such JSON without writing code, the JSON Formatter I built keeps large numbers exactly as written (it says how many it protected), and the JSON Parser warns about every integer that is too large for JavaScript. Both run entirely in the browser.

Summary

Situation What to do
You control the API Send IDs as strings
You must read numeric IDs in JS JSON.parse reviver with context.source and BigInt
You must write them back JSON.rawJSON
Long decimals in Python json.loads(..., parse_float=Decimal)

The scary part of this bug is the silence. If your app handles IDs from Twitter, Discord, a snowflake generator or a bigint column, it is worth checking today.

Have you been bitten by this one? I'd like to hear how you found it.

Top comments (2)

ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
sunnatulla_dev profile image
Sunnatulla •

Fix 2 has a sharp edge if the backend ever moves to Fix 1. On Node 22 the reviver throws SyntaxError: Cannot convert "1849302746519283746" to a BigInt, because context.source keeps the quotes. A null id throws too. Guarding it with typeof value === 'number' made all three cases parse for me.

ðŸ”Ĩ āļ‚āđˆāļēāļ§āļ”āđˆāļ§āļ™: mariabkms/jsonparse is silently changing your ids and how to stop it ng - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
mujtbabbas profile image
Syed Mujtaba •

The Prisma and MySQL version of this bites at the other end first. A BIGINT column comes back as a JS BigInt, and JSON.stringify throws on it, so the API layer ends up converting IDs to strings before anything leaves the process. That failure is loud, which makes it the lucky one. The silent version is an ID that arrives as a JSON number from another service and gets parsed in Node. Test fixtures almost never catch it because they use IDs like 42. Do you run the round-trip check against production-shaped IDs, or is it a manual check when a new ID source shows up?