Home > Blockchain >  jq: divide a decimal number but keep precision
jq: divide a decimal number but keep precision

Time:10-08

I have some JSON data that looks like this:

{
  "entries": [
    {
      "frame": 1,
      "timestamp": 0,
      "r": 130,
      "g": 105,
      "b": 99
    },
    {
      "frame": 2,
      "timestamp": 99.96299999999974,
      "r": 129,
      "g": 105,
      "b": 99
    },
...
}

I've tried running jq '.entries[].timestamp = .entries[].timestamp/1000.0' data.json but the outcome has lost precision on the values. I'm getting:

{
  "entries": [
    {
      "frame": 1,
      "timestamp": 0,
      "r": 130,
      "g": 105,
      "b": 99
    },
    {
      "frame": 2,
      "timestamp": 0,
      "r": 129,
      "g": 105,
      "b": 99
    },
...
}

Have I missed something obvious here? Is there a different way to do this?

CodePudding user response:

You didn't lose precision, you just created more results.

Use the update operator |= instead. In this case, you could even go with /=.

jq '.entries[].timestamp /= 1000.0' data.json

Try it on jqplay.org

  • Related