TypeError: BigInt value can't be serialized in JSON

The JavaScript exception "BigInt value can't be serialized in JSON" occurs when a BigInt is encountered in JSON.stringify with no custom serialization method provided.

Message

TypeError: Do not know how to serialize a BigInt (V8-based)
TypeError: BigInt value can't be serialized in JSON (Firefox)
TypeError: JSON.stringify cannot serialize BigInt. (Safari)

Error type

TypeError

What went wrong?

You are trying to serialize a BigInt value using JSON.stringify, which does not support BigInt values by default. Sometimes, JSON stringification happens implicitly in libraries, as part of data serialization. For example, sending data to the server, storing it in external storage, or transferring it between threads would all require serialization, which is often done using JSON.

There are several ways to handle this:

For more information on various tradeoffs, see BigInt reference.

Examples

undefined

Providing a custom serialization method

By default, BigInt values are not serializable in JSON:

js
const data = { a: 1n };
JSON.stringify(data);
// TypeError: BigInt value can't be serialized in JSON

Assuming you intend for the JSON to contain a number value, here are a few approaches that work:

See also