jq reformatting decimals in scientific notation -- can this be avoided? jq reformatting decimals in scientific notation -- can this be avoided? json json

jq reformatting decimals in scientific notation -- can this be avoided?


You can't change jq's behavior -- at present date, relevant feature requests are still open -- but you can reformat your numbers after they've been retrieved. For example:

json='{"decimal":0.00001}'decimal=$(jq '.decimal' <<<"$json")decimal_f=$(awk -v decimal="$decimal" 'BEGIN { printf("%f\n", decimal) }' </dev/null)echo "JQ emitted $decimal; reformatted as $decimal_f"


Also, you can reformat your JSON using perl module JSON::PP.

perl -0777 -MJSON::PP -E '$s=<>; $j=JSON::PP->new->ascii->pretty->allow_nonref->allow_bignum;$p=$j->decode($s);say $j->encode($p)'

or nicer:

perl -0777 -MJSON::PP -E '    $j=JSON::PP->new->ascii->pretty->allow_nonref->allow_bignum;    $p=$j->decode(<>);    say $j->encode($p)'

The crucial is the allow_bignum.

Example:

echo '{"decimal":0.00000001}' | perl ....

prints

{   "decimal" : 0.00000001}

but without the allow_bignum prints

{   "decimal" : 1e-08}

Ps: ... and also, is possible to validate the whole json using perl... :)


A commit made on Oct 21, 2019, ensures that jq will generally preserve the "external" format of numbers. There are some exceptions, e.g. superfluous leading 0s.

The following page gives details about installing an "unreleased" version of jq: https://github.com/stedolan/jq/wiki/Installation