Skip to content

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:

json
[
  {"id": 1850254339563143168, "user": "alice", "likes": 42},
  {"id": 1850254339563143169, "user": "bob", "likes": 7},
  {"id": 1849000000000000001, "user": "carol", "likes": 19}
]

Parse it with Node.js:

bash
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:

bash
fx tweets.json .
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:

bash
fx tweets.json '@(typeof x.id)'
json
[
  "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:

bash
fx tweets.json '@.id + 1n'
json
[
  1850254339563143169,
  1850254339563143170,
  1849000000000000002
]

Filter JSON by a Large ID ​

Use === with a BigInt literal:

bash
fx tweets.json '?.id === 1850254339563143169n' '@.user'
json
[
  "bob"
]

WARNING

Without the n, the literal is a JavaScript number and gets rounded before the comparison. With == it then matches the wrong record:

bash
fx tweets.json '?.id == 1850254339563143169' '@.user'
json
[
  "alice"
]

The same works on newline-delimited JSON, one value at a time. Return skip to drop a value:

json
{"guild_id": 1102938475610293847, "event": "join", "user_id": 998877665544332211}
{"guild_id": 1102938475610293848, "event": "leave", "user_id": 998877665544332212}
{"guild_id": 1102938475610293847, "event": "message", "user_id": 998877665544332213}
bash
fx events.ndjson 'x.guild_id === 1102938475610293847n ? x : skip' '.user_id'
998877665544332211
998877665544332213

Sort JSON Array by Big Integer ID ​

sortBy compares values with < and >, which work on BigInts:

bash
fx tweets.json 'sortBy(x => x.id)' '@.user'
json
[
  "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:

bash
fx tweets.json '@.id' 'x.sort((a, b) => a < b ? -1 : a > b ? 1 : 0)'
json
[
  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:

json
[
  {"account": "ops", "balance": 9007199254740993},
  {"account": "billing", "balance": 250},
  {"account": "payroll", "balance": 12000000000000000000}
]
bash
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 conversions

Wrap each value in BigInt() so every operand is a BigInt:

bash
fx accounts.json '@.balance' 'x.reduce((a, b) => a + BigInt(b), 0n)'
12009007199254741243

This 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:

bash
fx events.ndjson 'walk(v => typeof v === "bigint" ? String(v) : v)'
json
{
  "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():

json
[
  {"id_str": "1850254339563143169", "text": "hello"},
  {"id_str": "1850254339563143170", "text": "world"}
]
bash
fx strs.json '@(BigInt(x.id_str) - 1n)'
json
[
  1850254339563143168,
  1850254339563143169
]

See Also ​