【问题标题】:Parsing AWS JSON with JQ使用 JQ 解析 AWS JSON
【发布时间】:2020-11-07 06:13:34
【问题描述】:

我正在尝试解析来自 AWS CLI 的 JSON 输出。我正在寻找的是下面带有特定标签的安全组名称。两个有效的命令是

$aws ec2 describe-security-groups | jq  -r '.SecurityGroups[].GroupName'
default
mysqlsg
apachesg
default

然后我跑

$ aws ec2 describe-security-groups | jq  -r '.SecurityGroups[].Tags[]|select(.Key == "Service")'
{
  "Key": "Service",
  "Value": "default"
}
{
  "Key": "Service",
  "Value": "MySQL"
}
{
  "Key": "Service",
  "Value": "Apache"
}
{
  "Key": "Service",
  "Value": "default"
}

我希望每个组的下方都有服务标签,所以我尝试了这个,但没有成功。

$ aws ec2 describe-security-groups | jq  -r '.SecurityGroups[].GroupName,.SecurityGroups[].Tags[]|select(.Key == "Service")'
jq: error (at <stdin>:225): Cannot index string with string "Key"

【问题讨论】:

  • “我希望每个组都有服务标签”是什么意思">
  • 每个安全组都有 Key Service 的标签。我想显示组名称以及具有密钥服务的标签及其下方的值。

标签: amazon-web-services jq aws-cli


【解决方案1】:

您可以使用aws-cli 查询参数执行此操作,尝试以下操作,它应该可以工作。

aws ec2 describe-security-groups --query 'SecurityGroups[].{Tags:Tags[?Key==`Name`].Value|[0],GroupName:GroupName}'

输出

    {
        "Tags": "demo",
        "GroupName": "demo"
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多