大网数据广告活动
Web 标准与前端开发

TypeError: BigInt value can't be serialized in JSON

· 52CE 我爱测

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.

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:

  • If you can alter the data source, avoid using BigInt values and cast it to a number first (which may lose precision for large numbers).
  • If you can alter the stringification process, pass a replacer function to JSON.stringify that converts BigInt values to strings or numbers.
  • You can also provide a BigInt.prototype.toJSON method globally that gets called whenever a BigInt value is stringified.

For more information on various tradeoffs, see BigInt reference.

Examples

Providing a custom serialization method

By default, BigInt values are not serializable in JSON:

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:

  • Convert the BigInt to a number before stringifying:

    const data = { a: 1n };
    JSON.stringify({ ...data, a: Number(data.a) });
    // '{"a":1}'
    
  • Provide a replacer function that converts BigInt values to numbers or raw JSON objects:

    const data = { a: 1n };
    JSON.stringify(data, (key, value) =>
      typeof value === "bigint" ? Number(value) : value,
    );
    // '{"a":1}'
    
    const data = { a: 1n };
    JSON.stringify(data, (key, value) =>
      typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value,
    );
    // '{"a":1}'
    
  • Provide a BigInt.prototype.toJSON method that gets called whenever a BigInt value is stringified:

    BigInt.prototype.toJSON = function () {
      return Number(this);
    };
    const data = { a: 1n };
    JSON.stringify(data);
    // '{"a":1}'
    
    BigInt.prototype.toJSON = function () {
      return JSON.rawJSON(this.toString());
    };
    const data = { a: 1n };
    JSON.stringify(data);
    // '{"a":1}'
    

See also

  • BigInt
  • JSON.stringify()
  • JSON.rawJSON()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。