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

I found the difference between json data created by JavaScipt and through jqwith bash (and other programming languages). With JavaScript, I can create decimal numbers with six digits after the period, even when I use float(). But with jqits difference, because adding a decimal value takes only four digits after the decimal point.

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

Example:

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

My goal is to check the decimal with this string ...

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

Any feedback / recommendation?

+4
source share
2 answers

jq - , - , . :

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"
+4

, JSON perl 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)'

:

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

allow_bignum.

:

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

{
   "decimal" : 0.00000001
}

allow_bignum

{
   "decimal" : 1e-08
}

Ps:... json perl...:)

+3

Source: https://habr.com/ru/post/1672940/


All Articles