简体   繁体   English

使用jq循环遍历json以获取多个值

[英]Loop through json using jq to get multiple value

Here is volumes.json : 这是volumes.json:

{
"Volumes": [
    {
        "AvailabilityZone": "us-east-1a",
        "Tags": [
            {
                "Value": "vol-rescue-system",
                "Key": "Name"
            }
        ],
        "VolumeId": "vol-00112233",
    },
    {
        "AvailabilityZone": "us-east-1a",
        "Tags": [
            {
                "Value": "vol-rescue-swap",
                "Key": "Name"
            }
        ],
        "VolumeId": "vol-00112234",
    },
    {
        "AvailabilityZone": "us-east-1a",
        "Tags": [
            {
                "Value": "vol-rescue-storage",
                "Key": "Name"
            }
        ],
        "VolumeId": "vol-00112235",
    }
]
}

I need to get both the value of VolumeId and Tags.Value to be used as the input to invoke another command. 我需要将VolumeIdTags.Value的值都用作调用另一个命令的输入。 It is easy to get a single value from the json array, but I am not able to extract multiple value from it and pass it to another bash command. 从json数组中获取单个值很容易,但是我无法从中提取多个值并将其传递给另一个bash命令。

I can get a single value using this: 我可以使用这个得到一个值:

cat volumes.json |jq -r '.Volumes[].VolumeId' |while read v; do another_bash_command $v; done

but I am not able to get multiple value cause this is wrong: 但我无法获得多重值因为这是错误的:

 cat volumes.json |jq -r '.Volumes[].VolumeId, .Volumes[].Tags[].Value' |while read v w; do another_bash_command $v $w; done

as it will then loop 6 times of the outcome instead of 3. 因为它将循环6次结果而不是3次。

And , how do I pass those multiple json value in the loop to a bash array so I can use the value in a better way ? 并且 ,如何将循环中的多个json值传递给bash数组,以便我可以更好地使用该值? Like VolumeId-> $arr[0][0] , Tags.Value-> $arr[0][1] , AvailabilityZone-> $arr[0][2] ...etc. VolumeId-> $arr[0][0]Tags.Value-> $arr[0][1]AvailabilityZone-> $arr[0][2] ......等。 I have searched through SO and the jq docs, and tried readarray , but still not able to find out the solution :( Thanks for any help given. 我搜索了SO和jq文档,并尝试了readarray ,但仍然无法找到解决方案:(感谢您给予的任何帮助。

It seems to me that you want to output the two values ( VolumeId and Tags[].Value ) on the same line? 在我看来,你想在同一行输出两个值( VolumeIdTags[].Value VolumeId )?

If that's the case, then a simple string concatenation should be enough: 如果是这种情况,那么简单的字符串连接就足够了:

$ jq -r '.Volumes[] | .VolumeId + " " + .Tags[].Value' volumes.json
vol-00112233 vol-rescue-system
vol-00112234 vol-rescue-swap
vol-00112235 vol-rescue-storage

The above can then be used in a pipeline with while-read : 以上可以在while-read的管道中使用:

$ cat my_script
jq -r '.Volumes[] | .VolumeId + " " + .Tags[].Value' volumes.json \
| while IFS= read -r volumeId tagValue; do
  other_command "$volumeId" "$tagValue"
done

You should note that if there is more than one element in Tags the result will reflect that. 您应该注意,如果Tags有多个元素,结果将反映出来。 This can however be avoided by referring the first element in Tags : .Tags[0].Value 但是,可以通过引用Tags.Tags[0].Value的第一个元素来避免这种情况

As @andlrc observed, you may need to decide what you really want in the event that any Tags array has more or less than one element. 正如@andlrc观察到的那样,如果任何Tags数组包含多于或少于一个元素,您可能需要决定您真正想要的是什么。 Assuming you want Tags[0] in all cases, I would recommend considering the use of @tsv as follows: 假设您在所有情况下都需要Tags[0] ,我建议您考虑使用@tsv,如下所示:

jq -r '.Volumes[] | [.VolumeId, .Tags[0].Value] | @tsv' volumes.json

This would be especially appropriate if any of the .VolumeId or .Tags[0].Value values contained spaces, tabs, newlines, etc. The point is that @tsv will handle these in a standard way, so that handling the pair of values can be done in a standard way as well. 如果任何.VolumeId.Tags[0].Value值包含空格,制表符,换行符等,这将是特别合适的。关键是@tsv将以标准方式处理这些,以便处理这对值也可以以标准方式完成。 Eg using awk, you could read in the pair with awk -F\\\\t ; 例如,使用awk,你可以用awk -F\\\\t来读取这对; using bash, IFS=$'\\t' , etc. 使用bash, IFS=$'\\t'等。

我知道问题是如何使用jq获取信息,但也可以使用--query标志直接从aws cli获取预期参数。

aws ec2 describe-volumes --query "Volumes[].[VolumeId, Tags[].Value]" --output text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM