使用jq过滤掉元素的特定json个数组字段
Filter out specific json array fields of elements using jq
我 运行 一个 aws rds describe-instances
我得到了一个 json
实例数组,大致如下:
"DBInstances": [
{
"DBInstanceIdentifier": "my-rds",
"Endpoint": {
"HostedZoneId": "XXXXXXXXXXXX"
},
"DBSecurityGroups": [],
"VpcSecurityGroups": [
{
"VpcSecurityGroupId": "sg-12345",
"Status": "active"
},
{
"VpcSecurityGroupId": "sg-12345",
"Status": "active"
}
],
"DBParameterGroups": [
{
"DBParameterGroupName": "postgres-12",
"ParameterApplyStatus": "in-sync"
}
],
"AvailabilityZone": "us-west-1c",
"DBSubnetGroup": {
"VpcId": "vpc-123456",
"SubnetGroupStatus": "Complete",
"Subnets": [
{
"SubnetIdentifier": "subnet-123456",
"SubnetAvailabilityZone": {
"Name": "us-west-1b"
},
"SubnetOutpost": {},
"SubnetStatus": "Active"
},
{
"SubnetIdentifier": "subnet-12345",
"SubnetAvailabilityZone": {
"Name": "us-west-1c"
},
"SubnetOutpost": {},
"SubnetStatus": "Active"
},
{
"SubnetIdentifier": "subnet-12345",
"SubnetAvailabilityZone": {
"Name": "us-west-1e"
},
"SubnetOutpost": {},
"SubnetStatus": "Active"
}
]
},
"EngineVersion": "11.9",
"AutoMinorVersionUpgrade": true,
"ReadReplicaDBInstanceIdentifiers": [],
"LicenseModel": "foobar",
"OptionGroupMemberships": [
{
"OptionGroupName": "randomname",
"Status": "in-sync"
}
],
},
}
输出t运行很明显。
有没有办法使用(jq
可能?)来打印:
a) DBInstances
数组的所有元素
并且只为每个元素打印
b) DBInstanceIdentifier
和 EngineVersion
?
已解决,不是 .jq
,而是 aws
cli 功能:
aws rds describe-db-instances --query 'DBInstances[*].[DBInstanceIdentifier, EngineVersion]'
您可以使用以下 JQ 过滤器:
.DBInstances[] | [ .DBInstanceIdentifier, .EngineVersion ]
在哪里
.DBInstances[]
遍历 DBInstances
数组
[ .DBInstanceIdentifier, .EngineVersion ]
创建一个仅包含 DBInstanceIdentifier
和 EngineVersion
键的数组
jq '.DBInstances[] | [ .DBInstanceIdentifier, .EngineVersion ]'
JqPlay Demo
根据您当前的 JSON 示例(DBInstances
数组中只有 1 个项目),结果是:
[
"my-rds",
"11.9"
]
我 运行 一个 aws rds describe-instances
我得到了一个 json
实例数组,大致如下:
"DBInstances": [
{
"DBInstanceIdentifier": "my-rds",
"Endpoint": {
"HostedZoneId": "XXXXXXXXXXXX"
},
"DBSecurityGroups": [],
"VpcSecurityGroups": [
{
"VpcSecurityGroupId": "sg-12345",
"Status": "active"
},
{
"VpcSecurityGroupId": "sg-12345",
"Status": "active"
}
],
"DBParameterGroups": [
{
"DBParameterGroupName": "postgres-12",
"ParameterApplyStatus": "in-sync"
}
],
"AvailabilityZone": "us-west-1c",
"DBSubnetGroup": {
"VpcId": "vpc-123456",
"SubnetGroupStatus": "Complete",
"Subnets": [
{
"SubnetIdentifier": "subnet-123456",
"SubnetAvailabilityZone": {
"Name": "us-west-1b"
},
"SubnetOutpost": {},
"SubnetStatus": "Active"
},
{
"SubnetIdentifier": "subnet-12345",
"SubnetAvailabilityZone": {
"Name": "us-west-1c"
},
"SubnetOutpost": {},
"SubnetStatus": "Active"
},
{
"SubnetIdentifier": "subnet-12345",
"SubnetAvailabilityZone": {
"Name": "us-west-1e"
},
"SubnetOutpost": {},
"SubnetStatus": "Active"
}
]
},
"EngineVersion": "11.9",
"AutoMinorVersionUpgrade": true,
"ReadReplicaDBInstanceIdentifiers": [],
"LicenseModel": "foobar",
"OptionGroupMemberships": [
{
"OptionGroupName": "randomname",
"Status": "in-sync"
}
],
},
}
输出t运行很明显。
有没有办法使用(jq
可能?)来打印:
a) DBInstances
数组的所有元素
并且只为每个元素打印
b) DBInstanceIdentifier
和 EngineVersion
?
已解决,不是 .jq
,而是 aws
cli 功能:
aws rds describe-db-instances --query 'DBInstances[*].[DBInstanceIdentifier, EngineVersion]'
您可以使用以下 JQ 过滤器:
.DBInstances[] | [ .DBInstanceIdentifier, .EngineVersion ]
在哪里
.DBInstances[]
遍历DBInstances
数组[ .DBInstanceIdentifier, .EngineVersion ]
创建一个仅包含DBInstanceIdentifier
和EngineVersion
键的数组
jq '.DBInstances[] | [ .DBInstanceIdentifier, .EngineVersion ]'
JqPlay Demo
根据您当前的 JSON 示例(DBInstances
数组中只有 1 个项目),结果是:
[
"my-rds",
"11.9"
]