如何使jq将参数视为数字而不是字符串? [英] How to make jq treat argument as numeric instead of string?

查看:53
本文介绍了如何使jq将参数视为数字而不是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to make jq treat an input argument as numeric instead of string? In the following example, CURR_INDEX is a Bash variable which has array index value that I want to extract.

jq --arg ARG1 $CURR_INDEX '.[$ARG1].patchSets' inputfile.json

我收到以下错误:

jq: error: Cannot index array with string

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

I tried the workaround of using bash eval but some jq filters do not work properly in eval statements.

推荐答案

您可以将其转换为一个数字,例如:

jq --arg ARG1 1 '.[$ARG1|tonumber]' <<< '["foo". "bar"]'
"bar"

这篇关于如何使jq将参数视为数字而不是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆