代码之家  ›  专栏  ›  技术社区  ›  Levi

如何使用jq将多个输出对象组合成单个对象

  •  3
  • Levi  · 技术社区  · 7 年前

    我正在尝试解析terraform状态文件的输出。

    {
        "version": 3,
        "terraform_version": "0.9.3",
        "serial": 0,
        "lineage": "ae1f2572-8fa6-4977-be73-3deac7529eff",
        "modules": [
            {
                "path": [
                    "root"
                ],
                "outputs": {
                    "elb_dns_name": {
                        "sensitive": false,
                        "type": "string",
                        "value": "web-elb-1019323532.us-east-1.elb.amazonaws.com"
                    }
                },
                "resources": {},
                "depends_on": []
            },
            {
                "path": [
                    "root",
                    "elb"
                ],
                "outputs": {
                    "dns_name": {
                        "sensitive": false,
                        "type": "string",
                        "value": "web-elb-1019323532.us-east-1.elb.amazonaws.com"
                    }
                },
                "depends_on": []
            },
            {
                "path": [
                    "root",
                    "sg"
                ],
                "outputs": {
                    "security_group_id": {
                        "sensitive": false,
                        "type": "string",
                        "value": "sg-5a677425"
                    }
                },
                "depends_on": []
            },
            {
                "path": [
                    "root",
                    "web"
                ],
                "outputs": {
                    "web_instance_ids": {
                        "sensitive": false,
                        "type": "string",
                        "value": "i-03676fa6ba43fbb9f,i-09f51a313146856cd"
                    },
                    "web_public_ips": {
                        "sensitive": false,
                        "type": "string",
                        "value": "34.207.194.186,34.203.236.205"
                    }
                },
                "depends_on": []
            }
        ]
    }
    

    我想返回一个json对象,其中输出名称是键,值是输出值。就像在这个例子中。。。

    {
      "elb_dns_name": "web-elb-1019323532.us-east-1.elb.amazonaws.com",
      "dns_name": "web-elb-1019323532.us-east-1.elb.amazonaws.com",
      "security_group_id": "sg-5a677425",
      "web_instance_ids": "i-03676fa6ba43fbb9f,i-09f51a313146856cd",
      "web_public_ips": "34.207.194.186,34.203.236.205"
    }
    

    我只能用这个取回单个对象 .modules[] | .outputs | to_entries | map({(.key) : .value.value }) | add

    {
      "elb_dns_name": "web-elb-1019323532.us-east-1.elb.amazonaws.com"
    }
    {
      "dns_name": "web-elb-1019323532.us-east-1.elb.amazonaws.com"
    }
    {
      "security_group_id": "sg-5a677425"
    }
    {
      "web_instance_ids": "i-03676fa6ba43fbb9f,i-09f51a313146856cd",
      "web_public_ips": "34.207.194.186,34.203.236.205"
    }
    

    所以出于某种原因,我不能发布,因为stackoverflow说我有太多的代码,没有足够的细节。。。因此,现在我正在做一个日记条目,以便键入足够的内容,以便我可以点击提交按钮。。。随时。。。任何数量的字符现在。。。

    好吧,当我不在工作中构建代码管道时,我真的很喜欢玩dota2。只是我觉得我在这方面很差劲。我的意思是,我有1200个小时的游戏时间,但我仍然像1.5公里的MMR垃圾。

    2 回复  |  直到 7 年前
        1
  •  1
  •   peak    7 年前

    使用 map_values 生成更简单(或至少更短)的筛选器,并且可能更高效:

    .modules | map(.outputs | map_values(.value) ) | add
    
        2
  •  1
  •   Levi    7 年前

    我最终找到了一个解决方案,但所花的时间比应该的要长(几个小时)。基本上如果你在任何时候 , 在你的名单上,你把事情搞砸了。

    这是我用的 .modules | map(.outputs | to_entries[] | {(.key): .value.value}) | add

    这很有效,但也许是更好的方法。