Handling Large Integers in JSON Without Losing Precision
Twitter, Discord, and Snowflake-style IDs are 64-bit integers, and JSON.parse silently rounds them. fx reads any integer above 253 – 1 as a BigInt, so every digit survives. Install fx to follow along.
Why JSON Large Numbers Get Rounded in JavaScript
JavaScript numbers are 64-bit floats. Integers stay exact only up to Number.MAX_SAFE_INTEGER (9007199254740991). Take this tweets.json:
[
{"id": 1850254339563143168, "user": "alice", "likes": 42},
{"id": 1850254339563143169, "user": "bob", "likes": 7},
{"id": 1849000000000000001, "user": "carol", "likes": 19}
]Parse it with Node.js:
node -e 'const d = JSON.parse(require("fs").readFileSync("tweets.json", "utf8")); console.log(d.map(t => t.id))'[ 1850254339563143200, 1850254339563143200, 1849000000000000000 ]All three IDs are wrong, and alice and bob now have the same ID.
Pretty-Print JSON With Big Integers Intact
fx keeps the original digits:
fx tweets.json .[
{ "id": 1850254339563143168, "user": "alice", "likes": 42 },
{ "id": 1850254339563143169, "user": "bob", "likes": 7 },
{ "id": 1849000000000000001, "user": "carol", "likes": 19 }
]The interactive viewer (fx tweets.json) shows the exact values as well. To see which values fx parsed as BigInt, check their type:
fx tweets.json '@(typeof x.id)'[
"bigint",
"bigint",
"bigint"
]Small values like likes stay regular numbers. The switch happens at 9007199254740992. Negative numbers below -9007199254740991 also become BigInts.
Do Arithmetic on 64-bit IDs With BigInt
Add the n suffix to number literals so they are BigInts too:
fx tweets.json '@.id + 1n'[
1850254339563143169,
1850254339563143170,
1849000000000000002
]Filter JSON by a Large ID
Use === with a BigInt literal:
fx tweets.json '?.id === 1850254339563143169n' '@.user'[
"bob"
]WARNING
Without the n, the literal is a JavaScript number and gets rounded before the comparison. With == it then matches the wrong record:
fx tweets.json '?.id == 1850254339563143169' '@.user'[
"alice"
]The same works on newline-delimited JSON, one value at a time. Return skip to drop a value:
{"guild_id": 1102938475610293847, "event": "join", "user_id": 998877665544332211}
{"guild_id": 1102938475610293848, "event": "leave", "user_id": 998877665544332212}
{"guild_id": 1102938475610293847, "event": "message", "user_id": 998877665544332213}fx events.ndjson 'x.guild_id === 1102938475610293847n ? x : skip' '.user_id'998877665544332211
998877665544332213Sort JSON Array by Big Integer ID
sortBy compares values with < and >, which work on BigInts:
fx tweets.json 'sortBy(x => x.id)' '@.user'[
"carol",
"alice",
"bob"
]A custom comparator has to return a number. (a, b) => a - b returns a BigInt and fails with TypeError: Cannot convert a BigInt value to a number. Compare the values instead:
fx tweets.json '@.id' 'x.sort((a, b) => a < b ? -1 : a > b ? 1 : 0)'[
1849000000000000001,
1850254339563143168,
1850254339563143169
]Fix "Cannot Mix BigInt and Other Types"
A field can hold both types. Here balance is a BigInt in two records and a number in one, so adding the balances fails:
[
{"account": "ops", "balance": 9007199254740993},
{"account": "billing", "balance": 250},
{"account": "payroll", "balance": 12000000000000000000}
]fx accounts.json '@.balance' 'x.reduce((a, b) => a + b, 0n)'
@.balance x.reduce((a, b) => a + b, 0n)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Cannot mix BigInt and other types, use explicit conversionsWrap each value in BigInt() so every operand is a BigInt:
fx accounts.json '@.balance' 'x.reduce((a, b) => a + BigInt(b), 0n)'12009007199254741243This also goes the other way: .likes + 1n throws, because likes is a regular number. Use 1n only with BigInt values, or convert first with BigInt(x.likes). Converting a BigInt with Number() rounds it and loses precision.
Convert Large Integers to Strings for Other Tools
If the JSON goes to a browser, a Node.js service, or any parser that reads numbers as floats, send the IDs as strings. For one field, use '@({...x, id: String(x.id)})'. To convert every BigInt at any depth, use walk:
fx events.ndjson 'walk(v => typeof v === "bigint" ? String(v) : v)'{
"guild_id": "1102938475610293847",
"event": "join",
"user_id": "998877665544332211"
}
{
"guild_id": "1102938475610293848",
"event": "leave",
"user_id": "998877665544332212"
}
{
"guild_id": "1102938475610293847",
"event": "message",
"user_id": "998877665544332213"
}To go from strings back to BigInts, for example with the Twitter API's id_str, use BigInt():
[
{"id_str": "1850254339563143169", "text": "hello"},
{"id_str": "1850254339563143170", "text": "world"}
]fx strs.json '@(BigInt(x.id_str) - 1n)'[
1850254339563143168,
1850254339563143169
]