JQ 按子数组过滤 - 属性名称中带连字符
JQ filter by sub array - with hyphen in attribute name
我有以下数据文件:
[
{
"createTime": "2021-12-15T09:39:59.812377Z",
"etag": "\"15d377a6a77bab\"",
"labels": {
"is-secret": "false"
},
"name": "projects/xxx/secrets/xxx",
"replication": {
"automatic": {}
}
},
{
"createTime": "2021-12-19T03:47:53.594568Z",
"etag": "\"15d377a868c624\"",
"labels": {
"is-secret": "true"
},
"name": "projects/xxx/secrets/xxx",
"replication": {
"automatic": {}
}
}
]
我正在尝试 select 所有标签为 is-secret=true 的元素使用以下查询:
cat data.txt | jq '.[] | map(select( any(.labels[]; .is-secret == "true" )))'
输出需要是一个数组。
产量:jq:1个编译错误
有人可以帮忙吗?
干杯
可以直接对数组应用map(select(...))
,得到过滤后的数组:
jq 'map(select(.labels["is-secret"] == "true"))'
由于连字符,您还必须引用 is-secret
;方括号符号是可选的,但是 manual 使用的是什么;因此
jq 'map(select(.labels."is-secret" == "true"))'
也可以。
我有以下数据文件:
[
{
"createTime": "2021-12-15T09:39:59.812377Z",
"etag": "\"15d377a6a77bab\"",
"labels": {
"is-secret": "false"
},
"name": "projects/xxx/secrets/xxx",
"replication": {
"automatic": {}
}
},
{
"createTime": "2021-12-19T03:47:53.594568Z",
"etag": "\"15d377a868c624\"",
"labels": {
"is-secret": "true"
},
"name": "projects/xxx/secrets/xxx",
"replication": {
"automatic": {}
}
}
]
我正在尝试 select 所有标签为 is-secret=true 的元素使用以下查询:
cat data.txt | jq '.[] | map(select( any(.labels[]; .is-secret == "true" )))'
输出需要是一个数组。
产量:jq:1个编译错误
有人可以帮忙吗? 干杯
可以直接对数组应用map(select(...))
,得到过滤后的数组:
jq 'map(select(.labels["is-secret"] == "true"))'
由于连字符,您还必须引用 is-secret
;方括号符号是可选的,但是 manual 使用的是什么;因此
jq 'map(select(.labels."is-secret" == "true"))'
也可以。