如何在jq中将整数作为参数? [英] How can I put integer as an argument in jq?

查看:66
本文介绍了如何在jq中将整数作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用jq解析从aws cli返回的json文件,但是我一直困扰于使用索引号引用数组的问题.之所以需要这样做,是因为我想导出一个文本文件,以特定格式描述安全组,包括所有入站和出站规则.

I have been trying to use jq to parse a json file returned from the aws cli, but I'm stuck with the problem of referencing an array using the index number. I need to do this because I want to export a text file describing the security groups in a specific format, including all the inbound and outbound rules.

for (( i=1; i<=groupCount; i++ )) ; 
do
    echo $i
    echo $(echo "$input" | jq --arg i $i '.SecurityGroups[$i]')
done

这将返回错误:

1
jq: error (at <stdin>:189): Cannot index array with string "1"

2
jq: error (at <stdin>:189): Cannot index array with string "2"

3
jq: error (at <stdin>:189): Cannot index array with string "3"

有什么办法解决这个问题吗?

Is there any way around this?

推荐答案

您将不得不使用命令行arg --argjsonfromjson过滤器将参数转换为数字.数组只能通过int进行索引,并使用--arg将输入保留为字符串.

You would either have to use the command line arg --argjson or fromjson filter to convert the argument to a number. Arrays are may only be indexed by ints and using --arg keeps the input as a string.

$ jq --argjson i "$i" '.SecurityGroups[$i]'
$ jq --arg i "$i" '.SecurityGroups[$i|fromjson]'

这篇关于如何在jq中将整数作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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