gpt4 book ai didi

json - 如何使用 jq 将多个输出对象组合成一个对象

转载 作者:行者123 更新时间:2023-12-04 16:03:46 25 4
gpt4 key购买 nike

我正在尝试解析 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[] 取回单个对象 | .输出 |到条目 | map ({(.key):.value.value})|添加

{
"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 垃圾。

最佳答案

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

.modules | map(.outputs | map_values(.value) ) | add

关于json - 如何使用 jq 将多个输出对象组合成一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49680434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com