json jq:如何查询不包含文本“foo”的数组值?

jucafojl  于 5个月前  发布在  其他
关注(0)|答案(4)|浏览(59)

我有一个aws查询,我想过滤所有latest 结尾的imageTags
我试过这个,但它过滤了包含 latest 的东西,我想过滤不包含 latest 的东西(或者不**以 latest 结尾):

aws ecr describe-images --repository-name <repo> --output json \
  | jq '.[]' \
  | jq '.[]' \
  | jq "select ((.imagePushedAt < 14893094695) and (.imageTags[] | contains(\"latest\")))"

字符串

jv4diomz

jv4diomz1#

您可以使用not来反转逻辑

(.imageTags[] | contains(\"latest\") | not)

字符串
另外,我想您可以将管道简化为单个jq调用。

8aqjt8rx

8aqjt8rx2#

使用| not

一个有用的例子,特别是对于mac brew用户:

列出所有瓶装配方奶粉

通过查询JSON并解析输出,

brew info --json=v1 --installed | jq -r 'map(
    select(.installed[].poured_from_bottle)|.name) | unique | .[]' | tr '\n' ' '

字符串

列出所有非瓶装配方奶粉

通过查询JSON和解析输出,并使用| not

brew info --json=v1 --installed | jq -r 'map(                                                                                                                          
  select(.installed[].poured_from_bottle | not) | .name) | unique | .[]'

uajslkp6

uajslkp63#

在这种情况下contains()不能正常工作,最好使用index()not函数

select(.imageTags | index("latest") | not)

字符串

ar5n3qh5

ar5n3qh54#

这个.[] | .[]可以缩短为.[][],例如,

$ jq --null-input '[[1,2],[3,4]] | .[] | .[]'
1
2
3
4
$ jq --null-input '[[1,2],[3,4]] | .[][]'
1
2
3
4

字符串
要检查一个字符串是否不包含另一个字符串,您可以将合并containsnot组合在一起,例如,

$ jq --null-input '"foobar" | contains("foo") | not'
false
$ jq --null-input '"barbaz" | contains("foo") | not'
true


你可以用anyall对一个字符串数组做类似的事情,例如,

$ jq --null-input '["foobar","barbaz"] | any(.[]; contains("foo"))'
true
$ jq --null-input '["foobar","barbaz"] | any(.[]; contains("qux"))'
false
$ jq --null-input '["foobar","barbaz"] | all(.[]; contains("ba"))'
true
$ jq --null-input '["foobar","barbaz"] | all(.[]; contains("qux"))'
false


假设你有file.json:

[ [["foo", "foo"],["foo", "bat"]]
, [["foo", "bar"],["foo", "bat"]]
, [["foo", "baz"],["foo", "bat"]]
]


你只想保留没有任何字符串的嵌套数组"ba"

$ jq --compact-output '.[][] | select(all(.[]; contains("bat") | not))' file.json
["foo","foo"]
["foo","bar"]
["foo","baz"]

相关问题