【问题标题】:jq - How to filter a json that does not containjq - 如何过滤不包含的 json
【发布时间】:2017-08-02 11:27:11
【问题描述】:

我有一个要在 jq 中过滤的 aws 查询。 我想过滤所有以“latest”结尾的imageTags

到目前为止,我这样做了,但它过滤了包含“最新”的内容,而我想过滤不包含“最新”的内容(或不以“最新”结尾)

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

谢谢

【问题讨论】:

标签: json jq


【解决方案1】:

你可以使用not来反转逻辑

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

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

【讨论】:

  • 嘿,感谢您的回答,我尝试运行它,但我得到:aws ecr describe-images --repository-name embed-service --output json | jq '.[]' | jq '.[]' | jq "select (.imageTags[] | contains(\"latest\") | not)" jq: error: Cannot iterate over null 但我确实有没有 "latest" 的 imageTags
  • 多么奇怪。你有机会举一个你正在处理的 JSON 的例子吗?
  • 我缺少的心理差距是contains() 是一个终端过滤器,它只返回truefalse。要反转它,只需将其传送到not!事后看来是如此明显......
  • @GavrielFishel:该消息意味着某些图像没有标签。试试这个来修复它“aws ecr describe-images --repository-name embed-service --output json | jq '.[]?' | jq '.[]?' | jq "select (.imageTags[]?| contains(\"latest\") | not)"
  • 请注意,如果您正在进行多次评估,则必须在您正在反转的同一管道中进行。 .stuff[] | contains("thing") | not,不是(.stuff[] | contains("thing")) | not
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    $ 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"]
    

    【讨论】:

      【解决方案4】:

      您所要做的就是在您的jq 中添加| 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 | .[]"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        • 1970-01-01
        • 2021-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多