【发布时间】:2020-09-15 18:18:13
【问题描述】:
我有一个这样的 jq 命令:
jq '.SecurityGroups[] | {name: .GroupName, groupid: .GroupId, ips: .IpPermissions[].IpRanges[].CidrIp, sourcegroup: .IpPermissions[].UserIdGroupPairs[].GroupId}'
除了一件事,它运作良好。它似乎无法处理该值不存在的情况。仅当所有值都存在时才返回。
预期输出:
{
"name": "group1",
"groupid": "sg-012345",
"ips": "10.0.0.0/8",
"sourcegroup": "" # having this value not return at all would suffice
}
{
"name": "group2",
"groupid": "sg-06789",
"ips": "", # having this value not return at all would suffice
"sourcegroup": "sg-abcd"
}
{
"name": "group3",
"groupid": "sg-xyz",
"ips": "192.0.0.0/8",
"sourcegroup": "sg-xyz"
}
JSON 输入:
{
"SecurityGroups": [
{
"GroupName": "group1",
"IpPermissions": [
{
"IpRanges": [
{
"CidrIp": "10.0.0.0/8"
}
],
"UserIdGroupPairs": []
},
"GroupId": "sg-012345",
},
{
"GroupName": "group2",
"IpPermissions": [
{
"IpRanges": [],
"UserIdGroupPairs": [
{
"GroupId": "sg-abcd",
}
]
},
"GroupId": "sg-06789",
},
{
"GroupName": "group3",
"IpPermissions": [
{
"IpRanges": [
{
"CidrIp": "192.0.0.0/8"
}
],
"UserIdGroupPairs": [
{
"GroupId": "sg-xyz",
}
]
},
"GroupId": "sg-xyz"
}
]
}
我发现了以下内容,但我看不到它适用于我的情况,所以不确定我在这里做错了什么:
【问题讨论】:
-
这里的JSON输入无效。
标签: jq