Replace jq predefined variable with number

2.2k views Asked by At

jq could have predefined variables and replace with --arg options. It works well with string. However, I don't know how to replace the variable as number.

$jq -n --arg number 3000 '{"number":$number}'
{
  "number": "3000"
}

I would like to be able to generate something as following :

{
  "number": 3000
}

Thanks in advance.

2

There are 2 answers

0
peak On BEST ANSWER

Or:

jq -n --arg number 300 '{"number": $number|tonumber}'

or (as Jeff pointed out) with jq 1.5:

jq -n --argjson number 300 '{"number": $number}'

or (with versions of jq after June 26, 2015):

jq -n --arg number 300 '{$number} | .number |= tonumber'
0
Wenbing Li On

Here is the solution:

jq -n --argfile number <(printf '%d' 3000) '{"number":$number}'

Or

jq -n --arg number 3000 '{"number":$number|fromjson}'