对不同级别的值配对,基于邻居成员选择一个 [英] pair values from different levels selecting one based on neighbor member
问题描述
我正在尝试遍历带有jq的对象数组并将其转换为csv.我可以做一些选择,然后做csv部分,但是我正在努力解决的事情是弄清楚如何获取每个对象的Name
标记值.
I'm trying to go through an array of objects w/ jq and convert that to csv. I can do the some of the selection and to csv part but what I'm struggling with is figuring out how to get the Name
tag value of each object.
json看起来像这样:
The json looks like this:
{
"Groups": [],
"Instances": [
{
"InstanceType": "m5.xlarge",
"Tags": [
{
"Key": "jenkins_slave_type",
"Value": "demand_ec2 small"
},
{
"Key": "color",
"Value": "jenkins"
},
{
"Key": "role",
"Value": "jenkins"
},
{
"Key": "stack",
"Value": "dev-us-east-1"
},
{
"Key": "Name",
"Value": "worker-jenkins"
},
{
"Key": "secondary-role",
"Value": "worker"
}
],
"VirtualizationType": "hvm",
"CpuOptions": {
"CoreCount": 2,
"ThreadsPerCore": 2
},
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"HibernationOptions": {
"Configured": false
},
"MetadataOptions": {
"State": "applied",
"HttpTokens": "optional",
"HttpPutResponseHopLimit": 1,
"HttpEndpoint": "enabled"
}
}
]
}
{
"Groups": [],
"Instances": [
{
"InstanceType": "t2.micro",
"Tags": [
{
"Key": "Description",
"Value": "test"
},
{
"Key": "Name",
"Value": "test-connection-cloud-sql"
}
],
"VirtualizationType": "hvm",
"CpuOptions": {
"CoreCount": 1,
"ThreadsPerCore": 1
},
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"HibernationOptions": {
"Configured": false
},
"MetadataOptions": {
"State": "applied",
"HttpTokens": "optional",
"HttpPutResponseHopLimit": 1,
"HttpEndpoint": "enabled"
}
}
]
}
我可以这样访问实例类型:
I can get to the instance type like this:
aws ec2 describe-instances | jq '.Reservations[] | {type: .Instances[].InstanceType}'
但我似乎无法到达Tags
中没有嵌套的Name
值.我已经这样做了,但是它仍然是嵌套的:
but I can't seem to get to the Name
value in Tags
thats not nested. I've done it this way but its still nested:
aws ec2 describe-instances | jq '.Reservations[] | {type: .Instances[].InstanceType, name: .Instances[].Tags[] | select (.Key == "Name")}'
推荐答案
展开Instances
一次,以避免组合爆炸.要将过滤器应用于select
的结果,只需将过滤器写在它旁边.
Expand Instances
once to avoid a combinatorial explosion. To apply a filter to the result(s) of select
, simply write the filter next to it.
.Reservations[].Instances[] | {InstanceType, name: .Tags[] | select(.Key == "Name") .Value}
这篇关于对不同级别的值配对,基于邻居成员选择一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!