如何使用jq

时间:2018-04-05 19:51:07

标签: json key transform jq

我正在尝试解析一个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.5k MMR垃圾。

2 个答案:

答案 0 :(得分:1)

我最终找到了一个解决方案,但它花了比应有的时间(小时)。基本上,如果你在任何时候丢失了你的列表中的,而不是你搞砸了某个地方。

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

它有效,但可能是一种更好的方式。

答案 1 :(得分:1)

使用map_values生成一个更简单(或至少更短)的过滤器,可能更有效:

.modules | map(.outputs | map_values(.value) ) | add
相关问题