Search code examples
jsonintegerjqbigintarbitrary-precision

How do I get jq to preserve bigint values?


I have a large JSON file that contains bigints with their full values--not rounded like JavaScript loves to do by default. We have a workaround to deal with the bigints in Node.js, but I'm trying to use jq (the command-line tool) to clean up our data. However, when I ran jq on our JSON file, it rounded all of our bigints.

Is there a way to use jq so that it doesn't round the bigints or is there perhaps another command-line tool that works on a Mac that I may use instead?


Solution

  • As of right now, the best jq has to offer with respect to JSON numbers is the "master" version, which preserves the external numerical value very well. The updates were made on or about 22 Oct 2019, and the "master" version of jq seems to be as safe to use as the most recent release (jq 1.6).

    Examples using a recent "master" version:

        jqMaster -n -M '
        [0000,
         10000000000000000000000000000000000000012, 
         1.0000000000000000000000000000000000000012, 
         1000000000000000000000000000000000000001210000000000000000000000000000000000000012,
         0.1e123456]'
    

    Output

    [
      0,
      10000000000000000000000000000000000000012,
      1.0000000000000000000000000000000000000012,
      1000000000000000000000000000000000000001210000000000000000000000000000000000000012,
      1E+123455
    ]