如何使jq将参数视为数字而不是字符串?

har*_*ish 5 bash jq

如何使jq输入参数作为数字而不是字符串?在下面的示例中,CURR_INDEX是一个Bash变量,它具有我要提取的数组索引值。

jq --arg ARG1 $CURR_INDEX '.[$ARG1].patchSets' inputfile.json
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

jq: error: Cannot index array with string
Run Code Online (Sandbox Code Playgroud)

我尝试了使用的解决方法,bash eval但某些jq过滤器在eval语句中无法正常工作。

hek*_*mgl 5

您可以将其转换为数字,如下所示:

jq --arg ARG1 1 '.[$ARG1|tonumber]' <<< '["foo". "bar"]'
"bar"
Run Code Online (Sandbox Code Playgroud)