Do not use jq when working with large number
2021-09-07
Categories: Programming
We use etcd to store application configuration. On production, config is loaded from json file by using Python.
I’m wondering if we can use jq to do that. So, I tried something like this:
1host=${ETCD_HOST:-127.0.0.1} 2port=${ETCD_PORT:-2379} 3for key in $(jq -r 'keys[]' "$1"); do 4 value=$(jq -r ".$key" -c "$1") 5 ETCDCTL_API=3 etcdctl --endpoints="$host:$port" put "$key" "$value" 6done
Config is loaded into etcd
but some values are not the same as in the JSON file.
What is going on?
Looking more closely I found out that jq
is rounding large number:
1$ echo 947295729583939162 | jq '.' 2947295729583939200
The reason is jq
converts the JSON numbers to IEEE 754 64-bit values and the largest number is 2^53 (9,007,199,254,740,992), same as JavaScript:
1$ node -e "console.log(Number.MAX_SAFE_INTEGER);" 29007199254740991
Please take a look at FAQ for more details.
Tags: jq json javascript bigint
Related Posts:
- Learning Rust by building Tetris: my favorite childhood game
- SICP Exercise 2.77: expected a procedure that can be applied to arguments, given #f
- A terminal UI for Taskwarrior
- A simple terminal UI for ChatGPT
- gocloud - writing data to a bucket: 403