Skip to main content

Why big integers change in jq, and how to keep them exact

jq 1.6 and older read every number into a 64-bit float, so an integer above 2^53 (9,007,199,254,740,992) is rounded the moment it is parsed. jq 1.7 and newer print a number exactly as it was written unless the filter does arithmetic on it, so jq . keeps a 64-bit id and .id + 0 rounds it. Run jq --version to see which rule applies to you.

Updated

Why JSON numbers lose digits above 2^53

The JSON grammar puts no limit on the length of a number. RFC 8259 leaves the limit to each implementation, and most parsers store numbers as IEEE 754 doubles. A double has a 53-bit significand, so it holds every integer up to 2^53 and, past that, only every second one, then every fourth. A parser that meets 9007199254740993, which is 2^53 + 1, stores the nearest double it has.

node -e 'console.log(JSON.parse("12345678901234567890"))'12345678901234567000node -e 'console.log(JSON.parse("9007199254740993"))'9007199254740992

That is Node v22.23.1. The first number lost its last three digits and the second became a different integer. The same thing happens to 64-bit database keys, snowflake ids, and nanosecond timestamps, which can all exceed 53 bits. Money amounts hit a related problem: 1.10 is printed back as 1.1, and sums pick up float error.

Which jq versions round big integers

The rule changed in jq 1.7, released on 2023-09-06.

jq versionNumber passed through untouchedNumber after arithmeticSource
1.6 and olderConverted to a double when read, so rounded above 2^53Double1.6 manual and jq FAQ; not run here
1.7 and 1.7.1Printed as writtenConverted to a double1.7 manual; not run here
1.8.0 and newerPrinted as written, also after unary minus, abs, and lengthConverted to a doubleNEWS for 1.8.0; run here on 1.8.2
How jq handles a number literal, by version

The 1.6 manual says "jq currently only has IEEE754 double-precision (64-bit) floating point number support." The jq FAQ says versions up to and including 1.6 "convert very large integers to a floating point approximation on being read" and that for them "the largest number that can be reliably used as an integer is 2^53 (9,007,199,254,740,992)". jq 1.6 was not run for this page, so those quotes stand in for its output.

The manual for 1.7 and 1.8 says: "When a number which originally was provided as a literal is never mutated until the end of the program then it is printed to the output in its original literal form." It also says that arithmetic on such a number "will trigger a conversion to the IEEE754 representation".

jq --versionjq-1.8.2

If this prints jq-1.6 or lower, the digits are gone before your filter runs and no filter can bring them back. Upgrade jq, or read the file with one of the tools that print numbers as written.

What jq 1.7 and newer keep, and what they change

These runs use jq 1.8.2 on a small file, ledger.json.

cat ledger.json{"entries":[{"id":9007199254740993,"amount":1.10},{"id":12345678901234567890,"amount":-0},{"id":123456789012345678901234567890,"amount":1e-7}],"count":1E2}jq -c . ledger.json{"entries":[{"id":9007199254740993,"amount":1.10},{"id":12345678901234567890,"amount":-0},{"id":123456789012345678901234567890,"amount":1E-7}],"count":1E+2}

Every id came out as written, and so did 1.10 and -0. Two numbers were respelled with the same value: 1e-7 became 1E-7 and 1E2 became 1E+2. jq 1.8.2 keeps every digit and still rewrites exponents, so a checksum of its output can differ from the input.

jq '.entries[].id + 0' ledger.json900719925474099212345678901234567000123456789012345680000000000000jq '.entries[0].amount, .entries[0].amount + 0' ledger.json1.101.1

Adding zero was enough to convert each id. On 1.8.2 the same happened with -, *, /, floor, round, and add over an array. With % the answer was wrong outright: .entries[0].id % 10 returned 2 for an id that ends in 3. max, min, sort, unique, abs, and unary minus left the literal alone.

jq '.entries[0].id == 9007199254740992' ledger.jsonfalsejq -c '.entries[] | select(.id == 9007199254740993)' ledger.json{"id":9007199254740993,"amount":1.10}

Comparison uses the full literal, so 2^53 + 1 does not equal 2^53 and select returns the right record. In JavaScript, 9007199254740993 === 9007199254740992 is true.

How to keep ids exact in jq 1.7 and newer

Treat an id as an opaque token. Select it, compare it, sort by it, and pass it along, and keep it out of arithmetic. When a shell variable or a CSV column needs the id as text, tostring is safe on jq 1.8.2 and returns the digits as written:

jq -c '[.entries[].id | tostring]' ledger.json["9007199254740993","12345678901234567890","123456789012345678901234567890"]

When the filter has to compute with big integers, use gojq. Its README says it "supports arbitrary-precision integer calculation while jq does not", and warns that floor, round, and the other math functions still convert to floating point. jaq 3.1.2, run here, returned all three ids intact from .entries[].id + 0. It printed -0 as 0.

If you control the API that produces the file, send ids as JSON strings. A string survives every parser on this page.

JSON.parse in Node and browsers, and Python's json module

A pipeline can round in a step other than jq. JSON.parse returns JavaScript numbers, which are doubles in Node and in every browser, so a script that parses a file and writes it back changes it:

node -e 'console.log(JSON.stringify(JSON.parse(require("fs").readFileSync("ledger.json", "utf8"))))'{"entries":[{"id":9007199254740992,"amount":1.1},{"id":12345678901234567000,"amount":0},{"id":1.2345678901234568e+29,"amount":1e-7}],"count":100}node -e 'console.log(JSON.parse("{\"id\":9007199254740993}", (key, value, context) => key === "id" ? BigInt(context.source) : value))'{ id: 9007199254740993n }

The first command changed all three ids, 1.10, -0, and 1E2. The second is the fix on Node v22.23.1: the reviver receives a third argument whose source is the number's original text, which MDN documents, and the id becomes a BigInt.

Python's json module reads integers into Python's unbounded int, so ids stay exact. Decimals go through float unless you pass parse_float:

python3 -c 'import json; print(json.loads("12345678901234567890"))'12345678901234567890python3 -c 'import json; print(json.loads("1.10"))'1.1python3 -c 'import json, decimal; print(json.loads("1.10", parse_float=decimal.Decimal))'1.10

That is Python 3.15.0. A json.load followed by json.dumps on ledger.json kept the three ids and rewrote the other numbers as 1.1, 0, 1e-07, and 100.0.

Frequently asked questions

Is jq safe for money amounts?

For reading and printing, yes on jq 1.7 and newer: 1.10 stays 1.10. Arithmetic runs on doubles, so jq -n '0.1 + 0.2' prints 0.30000000000000004 on jq 1.8.2. Add amounts up in a decimal type such as Python's decimal.Decimal, or store them as integers in minor units.

Why does jq 1.7 or newer still round my big integers?

Either the filter does arithmetic on the number, or jq was built without its decimal backend. The manual describes a --disable-decnum build option, and jq 1.8 has a builtin that reports it: jq -n have_decnum prints true when literals are kept (manual).

Does jq --argjson keep a big integer?

Yes on jq 1.8.2. jq -n --argjson id 9007199254740993 '$id' prints 9007199254740993, and select(.id == $id) matches the right record. $id + 1 prints 9007199254740992, because arithmetic converts the value. --arg passes a string, which is never rounded.

What happens to a number too big for a double, such as 1e1000?

Gronify 1.3.1 prints 1e1000 and jq 1.8.2 prints 1E+1000. Node's JSON.parse returns Infinity, which JSON.stringify writes back as null. Python's json.loads returns inf, which json.dumps writes as Infinity, and that is not valid JSON. In jq 1.8.2, adding zero to it gives 1.7976931348623157e+308.

Try it on your own JSON.

Gronify is open source and MIT licensed. Install it with npm or Homebrew, or read the source first.

Install GronifyView the repository