JSON BigInt Truncation Error
Fix JSON BigInt truncation when JSON.parse loses precision with large integers. Learn how to handle BigInt values in JSON.
What Does This Error Mean?
JSON BigInt truncation occurs when JSON.parse converts a large integer (beyond Number.MAX_SAFE_INTEGER, 9,007,199,254,740,991) to a JavaScript Number, losing precision. The parsed value will be rounded to the nearest representable Number.
Common Causes
API response includes 64-bit integer IDs (common in Discord, Stripe, etc.)
Snowflake IDs or timestamp-based IDs exceeding 53 bits
Large numerical values from scientific or financial systems
JSON.stringify() losing precision when serializing large numbers
Database auto-increment IDs exceeding Number.MAX_SAFE_INTEGER
How to Fix It
Use a reviver function with JSON.parse
Detect large integers and convert them to BigInt or strings during parsing.
function parseBigIntJSON(json) {
return JSON.parse(json, (key, value) => {
if (typeof value === "number" && !Number.isSafeInteger(value)) {
return BigInt(value)
}
return value
})
}
// Or preserve as string
function parsePreserveLargeNumbers(json) {
// Replace large numbers with quoted strings before parsing
return JSON.parse(
json.replace(/\b(\d{16,})\b/g, '"$1"')
)
}Use JSON.parse with BigInt support library
Use libraries like json-bigint that handle large numbers correctly.
const JSONbig = require("json-bigint")
// Parse preserving BigInt
const data = JSONbig.parse(largeJsonString)
console.log(data.id.toString()) // Full precision
// Stringify BigInt values
const json = JSONbig.stringify(data)
// Or use lossless-json library
// const { parse, stringify } = require("lossless-json")Receive IDs as strings from the API
Ask the API provider to return large IDs as strings instead of numbers.
// ❌ API returns ID as number
{ "id": 12345678901234567890 }
// JSON.parse truncates: 12345678901234567000
// ✅ API returns ID as string
{ "id": "12345678901234567890" }
// JSON.parse preserves: "12345678901234567890"Use a custom JSON.stringify for BigInt
JavaScript BigInt values are not serializable by JSON.stringify by default.
// ❌ Error: Do not know how to serialize a BigInt
JSON.stringify({ id: BigInt("12345678901234567890") })
// ✅ Custom BigInt serialization
const data = { id: BigInt("12345678901234567890") }
const json = JSON.stringify(data, (key, value) =>
typeof value === "bigint" ? value.toString() : value
)Related Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
JSON Unexpected Token
Learn what causes the 'JSON Parse error: Unexpected token' error and how to fix it in JavaScript, Python, and other languages.
JSON Unexpected End of Input
Learn why 'Unexpected end of JSON input' occurs and how to fix truncated or incomplete JSON data.
JSON Circular Reference Error
Fix 'Converting circular structure to JSON' error when using JSON.stringify with objects that reference themselves.