Mario
Mario

Reputation: 1042

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

I found difference between json-data created by JavaScipt and via jq with bash (and other programming languages). With JavaScript I can create decimal numbers with up to six digits after the point, even when I use float(). But with jq its different, because adding a decimal value takes four digits after the decimal point only.

My problem is that I need decimal numbers to store in SQL, with up to six digits after the point.

Example:

$ JSON='{"decimal":0.00001}'
$ echo "$JSON" | jq .
{
  "decimal": 1e-05
}

My goal is to validate the decimal with this line ...

if [[ "$TMP_DECIMAL" =~ ^[0-9]+([.][0-9]+)?$ ]] ; then

Any tip / suggsestion ?

Upvotes: 9

Views: 4785

Answers (3)

peak
peak

Reputation: 116900

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

Upvotes: 0

clt60
clt60

Reputation: 63962

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... :)

Upvotes: 3

Charles Duffy
Charles Duffy

Reputation: 295679

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"

Upvotes: 9

Related Questions