> > > > >
JSONRepair

How to Fix "Do Not Know How to Serialize a BigInt" in JSON.stringify

You never typed a BigInt literal, yet JSON.stringify threw “Do not know how to serialize a BigInt”. The value arrived as one, usually a database id. Here is how to fix it: find the key, then pick from three fixes based on what the value is for.

The error message changes with the engine, which is half the confusion. In Chrome and Node (V8) it reads TypeError: Do not know how to serialize a BigInt. Firefox says BigInt value can't be serialized in JSON. Safari says JSON.stringify cannot serialize BigInt. Same failure, three wordings, so searching your exact message can return nothing useful while someone else's wording returns the answer. If you are trying to fix the "do not know how to serialize a BigInt" error, start by knowing all three names for it.

And here is the part that misleads people: you probably never typed a BigInt literal. The value arrived as one. Database drivers hand back BigInt for BIGINT columns, so an auto-increment id or a microsecond timestamp comes out of the query already a BigInt. ORMs do the same for 64-bit integers. Somewhere between the query and the stringify call, the type changed and nobody told you.

JSON itself is the reason there is no default. The format was specified before BigInt existed and has no notation for an integer too large for a double. A BigInt can be arbitrarily large, so the serializer refuses to guess. Note the asymmetry, because it says something about the language: Map and Set stringify to {} silently, dropping your data without a word. BigInt at least throws.

Find the BigInt before you fix anything

Run the stringify with a replacer that reports instead of converting:

JSON.stringify(data, (key, value) => {
  if (typeof value === "bigint") console.log("BigInt at key:", key);
  return value;
});

This still throws, but the log names the key carrying the BigInt. It is usually an id, a createdAt in microseconds, or a money amount in minor units. Which one it is decides the fix, because the right fix for a database id is different from the right fix for a token balance.

How to fix the "do not know how to serialize a BigInt" error: three options

The choice depends on what the value is for, so here are the three in the order I would consider them.

Convert it at the source with Number(). If the value will always fit in a normal number, this is one line and you are done:

JSON.stringify({ ...data, id: Number(data.id) });

The condition matters more than the line. Past 9,007,199,254,740,991 (2^53 − 1), a JavaScript number cannot represent every integer, and the conversion silently rounds. If the whole reason the value is a BigInt is exactness, a 64-bit Snowflake id or a wei amount, Number() reintroduces the rounding you were avoiding. My rule: use Number() only when you can state the maximum value out loud.

Convert BigInts to strings with a replacer. This is the general fix, and the one I reach for at API boundaries:

const safeStringify = (value) =>
  JSON.stringify(value, (key, v) =>
    typeof v === "bigint" ? v.toString() : v
  );

The JSON stays valid, the digits stay exact, and the consumer decides how to interpret the string. If you want the output to keep looking numeric, newer runtimes offer JSON.rawJSON(value.toString()), which emits the digits as a raw JSON number. Exactness still depends on the consumer parsing it as a BigInt on the other end.

Teach BigInt to serialize itself. JSON.stringify has a backdoor just for this type: it calls a BigInt's toJSON method when one exists. So this works:

BigInt.prototype.toJSON = function () {
  return this.toString();
};

Every stringify in the process now handles BigInt with no replacer. I use this in small scripts and CLIs where I control the whole runtime. I would not ship it in a library. You are mutating a shared builtin, and every consumer of that library inherits your serialization choice whether they want it or not.

The round-trip problem nobody mentions

Fixing the throw is half the job. On the way back in, JSON.parse hands you a string, and no reviver can reliably turn it back into a BigInt, because a serialized BigInt is indistinguishable from a string that was always a string. MDN's own docs flag this: the serialization is irreversible by inspection.

You have two honest options. Revive by key name, converting id back with BigInt(value), and accept that the key name is now load-bearing. Or use a tagged wrapper like {"$bigint": "123"} so the type travels with the value. The second is uglier and the first is more brittle. For anything crossing a service boundary, I take the tagged wrapper. At least the next person reading the payload can see what it is.

One more edge worth knowing: the throw can come from implicit stringification, not a stringify call you wrote. Logging libraries, error reporters, and frameworks that serialize props or state will hit it on your behalf. When the stack trace points at library code, the BigInt is still yours. The reporter just found it first.

Paste your broken JSON and get valid JSON back instantly, with a plain-English list of every fix.

Fix My JSON Now

Frequently asked questions

Why doesn't JSON.stringify support BigInt?

JSON was specified before BigInt existed and has no notation for an arbitrarily large integer. Rather than silently rounding your value into a double, the serializer throws and makes you choose a representation.

Is the error message the same in every browser?

No. V8 (Chrome, Node, Edge) says "Do not know how to serialize a BigInt", Firefox says "BigInt value can't be serialized in JSON", and Safari says "JSON.stringify cannot serialize BigInt". All three mean the same thing.

Will converting a BigInt to a Number lose precision?

Only past 2^53 − 1 (9,007,199,254,740,991). Below that boundary the conversion is exact. Above it, integers start rounding to the nearest representable double.

Can JSON.parse turn the string back into a BigInt automatically?

No. A reviver function can convert it back, but only by key name or with a tagged wrapper like {"$bigint": "123"}, because a serialized BigInt looks exactly like an ordinary string.

Is patching BigInt.prototype.toJSON safe?

It works, and MDN documents the backdoor that makes it work: stringify specifically calls toJSON on BigInt values. The risk is that it is a global mutation of a shared builtin, so keep it in application code you control, not in libraries.

Related reading: Why Does JSON Round Large Numbers? The 2^53-1 Precision Trap · How to Fix "Converting Circular Structure to JSON" · Unexpected Token 'o' in JSON at Position 1: You Parsed an Object, Not a String

One JSON bug a week, explained in plain English: subscribe to the newsletter.