使用jq遍历json以获取多个值 [英] Loop through json using jq to get multiple value

查看:152
本文介绍了使用jq遍历json以获取多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是volumes.json:

Here is 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",
    }
]
}

我需要同时获取VolumeIdTags.Value的值,以用作调用另一个命令的输入.从json数组中获取单个值很容易,但是我无法从中提取多个值并将其传递给另一个bash命令.

I need to get both the value of VolumeId and Tags.Value to be used as the input to invoke another command. 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.

我可以使用以下方法获得一个值:

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

因为它将随后循环6次而不是3次.

as it will then loop 6 times of the outcome instead of 3.

然后,如何将循环中的多个json值传递给bash数组,以便可以更好地使用该值?像VolumeId-> $arr[0][0]Tags.Value-> $arr[0][1]AvailabilityZone-> $arr[0][2] ...等.我已经搜索了SO和jq文档,并尝试了readarray,但仍然找不到解决方法:(感谢您提供的任何帮助.

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 ? Like VolumeId-> $arr[0][0], Tags.Value-> $arr[0][1], AvailabilityZone-> $arr[0][2]...etc. 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.

推荐答案

在我看来,您想在同一行上输出两个值(VolumeIdTags[].Value)?

It seems to me that you want to output the two values (VolumeId and Tags[].Value) on the same line?

如果是这样,那么简单的字符串串联就足够了:

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

然后可以在具有while-read的管道中使用以上内容:

The above can then be used in a pipeline with 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

您应注意,如果Tags中有多个元素,则结果将反映出来.但是,可以通过引用Tags中的第一个元素来避免此问题:.Tags[0].Value

You should note that if there is more than one element in Tags the result will reflect that. This can however be avoided by referring the first element in Tags: .Tags[0].Value

这篇关于使用jq遍历json以获取多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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