Json 如何在bash中使用jq过滤器过滤键和值

Json 如何在bash中使用jq过滤器过滤键和值,json,bash,amazon-web-services,jq,aws-cli,Json,Bash,Amazon Web Services,Jq,Aws Cli,在运行以下命令之后 aws ec2描述标记——过滤器“名称=资源id,值=i-8dh7435490fjksfd” 我有以下JSON输出 { "Tags": [ { "ResourceType": "instance", "ResourceId": "i-8dh7435490fjksfd", "Value": "production", "Key": "Environmen

在运行以下命令之后

aws ec2描述标记——过滤器“名称=资源id,值=i-8dh7435490fjksfd”

我有以下JSON输出

{
    "Tags": [
        {
            "ResourceType": "instance", 
            "ResourceId": "i-8dh7435490fjksfd", 
            "Value": "production", 
            "Key": "Environment"
        }, 
        {
            "ResourceType": "instance", 
            "ResourceId": "i-8dh7435490fjksfd", 
            "Value": "webserver", 
            "Key": "Application"
        }
    ]
}
如何使用jq过滤器获得以下输出


应用:不使用
jq

.Tags[]
| select(.Key == "Application")
| "\(.Key) : \(.Value)"
aws ec2 describe-tags   --filter "Name=resource-id,Values=i-8dh7435490fjksfd" --query 'Tags[?Key==`Application`].Value[]' --output text

不使用jq的解决方案

aws ec2 describe-tags   --filter "Name=resource-id,Values=i-8dh7435490fjksfd" --query 'Tags[?Key==`Application`].Value[]' --output text

您可以通过使用jq以这种方式获得它。在键周围加括号意味着它将作为表达式进行计算

cat example.json | jq '.[] | {(.[].Key): (.[].Value)}'
输出:

{
  "Environment": "production"
}
{
  "Environment": "webserver"
}
{
  "Application": "production"
}
{
  "Application": "webserver"
}

参考:

您可以通过使用jq以这种方式获得它。在键周围加括号意味着它将作为表达式进行计算

cat example.json | jq '.[] | {(.[].Key): (.[].Value)}'
输出:

{
  "Environment": "production"
}
{
  "Environment": "webserver"
}
{
  "Application": "production"
}
{
  "Application": "webserver"
}

Ref:

您不需要
jq
。使用CLI的
--query
选项。请参阅:您不需要
jq
。使用CLI的
--query
选项。见: