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"))'9007199254740992That 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 version | Number passed through untouched | Number after arithmetic | Source |
|---|---|---|---|
| 1.6 and older | Converted to a double when read, so rounded above 2^53 | Double | 1.6 manual and jq FAQ; not run here |
| 1.7 and 1.7.1 | Printed as written | Converted to a double | 1.7 manual; not run here |
| 1.8.0 and newer | Printed as written, also after unary minus, abs, and length | Converted to a double | NEWS for 1.8.0; run here on 1.8.2 |
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.2If 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.1Adding 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.10That 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.
Tools that print numbers exactly as written
When the job is to look at a value or search for it, use a tool that copies each number's text through unread. The gron family rewrites JSON as one path = value line per value. gron 0.7.1, fastgron 0.7.7, and Gronify 1.3.1 all printed every number in ledger.json unchanged, and so does jq 1.7 or newer when the filter is .. Exact numbers are not a reason to pick one of them over another.
gronify search ledger.json --regex "id|amount"json.entries[0].id = 9007199254740993json.entries[0].amount = 1.10json.entries[1].id = 12345678901234567890json.entries[1].amount = -0json.entries[2].id = 123456789012345678901234567890json.entries[2].amount = 1e-7gron ledger.json | grep amountjson.entries[0].amount = 1.10;json.entries[1].amount = -0;json.entries[2].amount = 1e-7;fastgron ledger.json | grep idjson.entries[0].id = 9007199254740993json.entries[1].id = 12345678901234567890json.entries[2].id = 123456789012345678901234567890gronify search flattens and filters in one streaming pass, so an id copied from a log is found digit for digit. Its options are in how to grep JSON, the line format is in the guide to flattening JSON, and the three gron tools are compared under gron alternatives. Gronify installs with npm install -g gronify on Node.js 20 or newer (install notes).
The trip back to JSON keeps the numbers too. gronify unflatten rebuilds the document from the lines, and for this file the result is byte-for-byte the input. The same check on jq -c . fails at the first respelled exponent.
gronify flatten ledger.json | gronify unflatten | cmp - ledger.json && echo identicalidenticaljq -c . ledger.json | cmp - ledger.jsonstdin ledger.json differ: char 138, line 1unflatten writes minified JSON and normalizes string escapes, so a pretty-printed input comes back with different whitespace and the same number text. gron-style tools do not compute. To select records by a condition or reshape the output, use jq 1.7 or newer and keep ids out of arithmetic. The guide to jq alternatives compares the two approaches.